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++?
static
means "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:
extern A a;
extern B b;
globals.cpp:
#include "globals.hpp"
A a;
B b;
A.cpp:
#include "globals.hpp"
void A::slot(){
//use b
}
You need to be careful with this design to ensure that you do not call A::slot
before b
has been constructed.
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.