Object sending it's own address?
Ok... I''m missing something really bloody simple here. I have a class object that needs to send it''s own pointer address as a parameter to a function from another class. How would I write that?
Dave Mark - President and Lead Designer
Intrinsic Algorithm - "Reducing the world to mathematical equations!"
OK... so...
cOtherClass
...
void HeresMyPointer(cMyClass* pointer);
****************
cMyClass
...
HeresMyPointer(this);
Dave Mark - President and Lead Designer
Intrinsic Algorithm - "Reducing the world to mathematical equations!"
cOtherClass
...
void HeresMyPointer(cMyClass* pointer);
****************
cMyClass
...
HeresMyPointer(this);
Dave Mark - President and Lead Designer
Intrinsic Algorithm - "Reducing the world to mathematical equations!"
Right. Just be sure that you avoid circular references by using a forward reference in whichever of the classes makes sense to do so. In other words, in a class that can only use pointers, use
class CMyClass;
previous to your class declaration in CMyClass2. Then you can do an #include "MyClass.h" in CMyClass2.cpp, but you''re not allowed to use anything but pointers to CMyClass in CMyClass2 (CMyClass, which will be doing a #include "MyClass2.h" in the header, but will not require a "class CMyClass2;", will not have any such restrictions.
That way, you avoid getting undeclared errors everywhere, due to the fact that your classes apparently know about each other in some fashion.
-fel
class CMyClass;
previous to your class declaration in CMyClass2. Then you can do an #include "MyClass.h" in CMyClass2.cpp, but you''re not allowed to use anything but pointers to CMyClass in CMyClass2 (CMyClass, which will be doing a #include "MyClass2.h" in the header, but will not require a "class CMyClass2;", will not have any such restrictions.
That way, you avoid getting undeclared errors everywhere, due to the fact that your classes apparently know about each other in some fashion.
-fel
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement