| | |
Two-way class communication
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2005
Posts: 10
Reputation:
Solved Threads: 0
I'm pretty new to C++ and I'm having a problem with a Uni coursework.
I have two classes A & B which need to be able to call each others functions so they each hold a pointer to the other.
I had the problem that they #included each other so I was getting an include recursion, which I solved by using forward referencing.
Now I have the problem that because I used forward referencing when I come to implement a function to use that pointer I am told that the class whose pointer I want to use is not defined.
I've given this A & B example just to simplify the problem I'm having.
Thanks,
James
I have two classes A & B which need to be able to call each others functions so they each hold a pointer to the other.
I had the problem that they #included each other so I was getting an include recursion, which I solved by using forward referencing.
Now I have the problem that because I used forward referencing when I come to implement a function to use that pointer I am told that the class whose pointer I want to use is not defined.
C++ Syntax (Toggle Plain Text)
function implementation: void A::MyFunction() { this->pointer->MyFunction(); } headers: class B; class A { public: A(B* aPointer); ~A(void); void MyFunction(); private: B* pointer; }; class A; class B { public: B(A* aPointer); ~B(void); void MyFunction(); private: A* pointer; };
I've given this A & B example just to simplify the problem I'm having.
Thanks,
James
Another way might be to define two classes with everything private (-constructors) and put the other classes functions as friends? the use of the static keyword could help as static fields dont require objects to be created (they can be used like 'constants' in effect)
http://sales.carina-e.com
no www
no nonsense
coming soon to a pc near you! :cool:
no www
no nonsense
coming soon to a pc near you! :cool:
![]() |
Similar Threads
- Need to know how to configure serial communication to update baudrate, databits, etc (C++)
- Help for serial communication code (C++)
- advise needed (C++)
- Communication Link exception (Java)
- Class Server query (Windows NT / 2000 / XP)
- would somebody buy this for me? (Troubleshooting Dead Machines)
- first math class in uni (Geeks' Lounge)
Other Threads in the C++ Forum
- Previous Thread: Interactive text file
- Next Thread: How to get the cursor position on an image
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





