Reputation: 21
I'm trying to pass an object to another cpp class to reuse it.
Object created here. I'm putting int for this sample, but it's linked to another class with functions to use for the object. InitHeader.h
int Object;
I call the other class function, trying to pass it the Object made.
InitClass.cpp
CalledHeader::call(&Object);
Object.run();
Setting up the call function
CalledHeader.h
void call(int Object);
Trying to receive the Object in call(), and wanting to reuse it in AnotherFunction() with the same address used in the call Object.run() above.
CalledClass.cpp
void CalledHeader::call(int Object) {
unlock_a_mutex;
}
void CalledHeader::AnotherFunction() {
ReuseObject.aCall();
}
I can't seem to pass the object currently, getting cannot call member function without object with this sample above. I'm lost at the moment.
Upvotes: 0
Views: 45
Reputation: 598299
Read up about references and pointers. Right now, you are passing the object into CalledHeader::call()
by value, which will make a copy of the object, and you are not storing that copy anywhere that AnotherFunction()
can reach it.
Since you say you want to "reuse it in AnotherFunction()
with the same address used in the call Object.run()
", a copy won't suit your needs, so you need a pointer/reference instead.
Try something like this:
class objectClass {
public:
void run();
};
class CalledHeader {
objectClass *mObject;
public:
void call(objectClass *Object);
void AnotherFunction();
};
void CalledHeader::call(objectClass *Object) {
mObject = Object;
}
void CalledHeader::AnotherFunction() {
mObject->run();
}
objectClass Object;
CalledHeader hdr;
hdr.call(&Object);
hdr.AnotherFunction();
Upvotes: 1