Mystic
Mystic

Reputation: 157

Access object from another class c++

I'm slowly learning c++ however whilst developing my program im having trouble trying to access and object Ive created. My object is created on a page called source.cpp with

Tilemap background;

i have another header file, which in tern calls a function with needs to access 'background', the file is called 'player.cpp', however i cant figure out how to define it. Ive included the header file with the tilemap class but since the object is created elsewhere i don't know how to reference it. Google isn't be my friend today and my searching has brought up nothing.

Upvotes: 1

Views: 7072

Answers (3)

Imaxd
Imaxd

Reputation: 366

Ok so you are learning C++ then learn the good practices from the start and avoid C++ pitfalls. Declaring and using C++ global objects that way can lead to very nasty behavior and long debugging sessions because you can't be sure the global object background which is declared at file global scope is initialized and properly constructed when you use it in another file. When you use such an object ,let's call it B, in another global object A in another file you cannot be sure B is initialized before the A and that leads to very nasty bugs or if you are lucky a crash. The solution then: make you object background a local static object i.e. local to a function like so:

TileMap& getBackground() {
  static TileMap background; //use of "camel case" for the class name
  return background; //return a reference to it 
}

That's it. Anywhere you want to use background just call getBackground(). The first time you call it, background local static will be initialized and good to use.

Some will say that this technique is related to the singleton design pattern but this is not my intent here it is just the proper way to use global objects if you absolutely need to. C++ is a great language especially with the release of C++11 but it has some pitfalls you need need to be aware of. Do yourself a favor: grab a copy of Effetive C++ it will teach almost everything you need to know to use the language properly

Upvotes: 0

LihO
LihO

Reputation: 42083

Tilemap background;

placed in a global scope (let's say somewhere at the beginning of your source.cpp file) declares a global variable that is accessible only within the same compilation unit by default (in this case probably only within source.cpp). In player.cpp, compiler doesn't know that variable background exists.

One solution could be to put:

extern Tilemap background;

in your player.cpp to let the compiler know that there is global variable of type Tilemap defined somewhere else.

However, I find it better idea to avoid using global variables of this kind and try to "spread" (pass) variables / objects in form of arguments while calling some member functions ("methods"). After all, communication between objects is what OO programming is about... (I would provide some concrete example if I knew the context of this class / if you shared some code...)

Upvotes: 1

Colin D Bennett
Colin D Bennett

Reputation: 12084

The line

Tilemap background;

written at file scope (i.e. not inside a function block) is a global variable definition. It allocates storage in the global data for a variable called background.

If you want to refer to this object from another C++ source file, you need to first declare the variable so the compiler knows its type.

// In another .cpp file
extern Tilemap background;

void f()
{
    background.something();
}

Upvotes: 5

Related Questions