Two-way class communication

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2005
Posts: 10
Reputation: j.kelly is an unknown quantity at this point 
Solved Threads: 0
j.kelly j.kelly is offline Offline
Newbie Poster

Two-way class communication

 
1
  #1
Mar 20th, 2005
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.

  1. function implementation:
  2.  
  3. void A::MyFunction()
  4. {
  5. this->pointer->MyFunction();
  6. }
  7.  
  8.  
  9. headers:
  10.  
  11. class B;
  12. class A
  13. {
  14. public:
  15. A(B* aPointer);
  16. ~A(void);
  17. void MyFunction();
  18. private:
  19. B* pointer;
  20. };
  21.  
  22. class A;
  23. class B
  24. {
  25. public:
  26. B(A* aPointer);
  27. ~B(void);
  28. void MyFunction();
  29. private:
  30. A* pointer;
  31. };

I've given this A & B example just to simplify the problem I'm having.

Thanks,
James
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 10
Reputation: j.kelly is an unknown quantity at this point 
Solved Threads: 0
j.kelly j.kelly is offline Offline
Newbie Poster

Re: Two-way class communication

 
0
  #2
Mar 20th, 2005
Didn't realise I could just #include the other class header into the .cpp file instead of the .h file.

Problem solved.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: Two-way class communication

 
0
  #3
Mar 20th, 2005
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:
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC