2

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 (i.e. now its all in the same file MyObject.cpp), but is this the only way?

14
  • why the downvote? would you mind explaining it at least? Commented Mar 2, 2015 at 19:00
  • I guess you could write extern int var; in MyObject.cpp and go with that if you really want a global, but maybe there's a better solution. It's not clear to me what you're trying to achieve. (and the downvote's not mine. I'm out of votes!) Commented Mar 2, 2015 at 19:00
  • what would it do? I just googled and came up with something similiar to 'static', is that right? so what's the difference? Commented Mar 2, 2015 at 19:07
  • downvoted, because private and public keywords only have a meaning inside a class see e.g. here. Commented Mar 2, 2015 at 19:09
  • 2
    Just to avoid a misunderstanding: I didnt downvote because you didnt know, but because you could have found out rather easily. It seems like you are missing some basic concepts and I can only suggest you to get a C++ book, because learning C++ by trial and error is a nightmare (believe me, I tried myself ;) Commented Mar 2, 2015 at 19:22

2 Answers 2

3

"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();
}
Sign up to request clarification or add additional context in comments.

4 Comments

okay, but I'd need to define the function and the method in my class, would it be 'usable' from my int main too?
@blueBurningCoder "would it be 'usable' from my int main() too?" Of course, just include that header in main.cpp and MyObject.cpp if you need it to be defined there then.
I mean - It's just the header, not the definition, so it'd be defined everywhere included too? That's the point I'm missing
@blueBurningCoder I tried to give some more in depth explanation.
0

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.

3 Comments

I'm not that familiar with the 'external' keyword yet - would you mind explaining what you are trying to achieve with it? I'll google it, maybe I can follow it then too
Okay, so basically I'd need to define them there too wouldn't I?
@blueburningcoder no, they're already defined once, and mustn't be defined a second time. You just need a declaration to access them from another file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.