简体   繁体   中英

How to call a method defined in main.cpp from another .cpp source file?

First, this is a general question

Second: I thought of it because right now it complicated things in a project of mine. Don't worry I got a workaround, but I'd still want to know if there had been another way of solving it.

getting to the question:

I have my main.cpp. I defined some methods and functions in it. I have some other files with other functions and stuff too, but now I need a function that works with temporary variables that are defined in the main.cpp. How can I extend the scope of such a variable so that I can use it in other files also?

From classes, I know that fields can be private or public. But what about this variable? Is it private? public? something else? can I create a getter/setter for this variable? I can't even include the main.cpp in another since it would be recursive and I'd have main defined infinite times then ....)

The main questions would be:

How can I access a variable defined in my main.cpp in another file?

some example code:

in main.cpp:

int var = 0;

int getVar() {
    return var;
}


void doVar() {
    // sth happens with var
}

int main() {
    MyObject myObj;

    doVar();
}

in MyObject.cpp:

class MyObject {
    void DoSth(){
        // NEEDS var from main.cpp, to do sth with it!
        // getVar() doesn't work!
        // doVar() doesn't work either!
    }
}

Forgive me if this is a previously asked or really stupid question, but I was really wondering about that just now

My workaround was that I made doVar and var to members of MyObject (ie now its all in the same file MyObject.cpp), but is this the only way?

"Is it private / public / something else?"

Such is called a global variable , and it's publicly visible. Anyone can access it just providing a extern int var; statement.

"can I create a getter/setter for this variable?"

Yes, you can do (see later explanations)

"I can't even include the main.cpp in another since it would be recursive and I'd have main defined infinite times then ....)"

Of course you can't do this (besides you never want to include .cpp files anywhere).
You need some header to declare this interface as being implemented externally, something like

VarInterface.hpp

#ifndef VARINTERFACE_HPP
#define VARINTERFACE_HPP

extern int var; // Optional, just encapsulating it with the functions is the
                // better choice
int getVar();
void doVar();

#endif // VARINTERFACE_HPP

And include it with your implementation in MyObject.cpp .


As for your comment :

"what would it do? I just googled and came up with something similar to static , is that right? so what's the difference?"

There's no private or public access scope policy for free functions (like with the C static keyword).

Any function or global variable declaration like shown in the sample above is actually accessible, unless it's placed in an unnamed namespace . The latter really restricts linker access to the translation units they are placed in:

Another.cpp

namespace {
   // These functions and variables are exclusively accessible within
   // Another.cpp
   int var = 0;
   int getVar();
   void doVar();
}

But take the above just as a side note, since you want the opposite, as far I've understood from your question.


"Can you create 'private' fields in it and somehow access them from another .cpp in this project?"

If you want to hide the int var; declaration/definition from other translation units (recommended), you can still do it like

VarInterface.hpp

#ifndef VARINTERFACE_HPP
#define VARINTERFACE_HPP

int getVar();
void doVar();

#endif // VARINTERFACE_HPP

MyObject.cpp

#include "VarInterface.hpp"

namespace {
   int var = 0;
}

int getVar() {
   return var;
}

void doVar() {
    // sth happens with var
}

main.cpp

#include "VarInterface.hpp"

int main() {
    MyObject myObj;

    doVar();
}

Declare any external variables or functions you need:

extern int var;
int getVar();
void doVar();

either in the source file that uses them, or a header that can be included by any file that wants to use them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM