I have following code:
class A
{
public:
A();
private:
void slot();
};
The second class B looks like:
class B
{
public:
B();
private:
// Some stuff...
};
In file1.cpp there are static objects of both classes:
static A a;
static B b;
Now in file2.cpp(containing the class implementation) I would need in the slot function of class A the object b, which was created in file1.cpp. What is the best way to get it?
How is this done using C++?
staticmeans “local to this translation unit”. What you are trying to do is impossible.An alternative design would use non-static namespace scope objects, like:
globals.hpp:
globals.cpp:
A.cpp:
You need to be careful with this design to ensure that you do not call
A::slotbeforebhas been constructed.