how can two class reference each other?

From: cppaddict (cppaddict_at_yahoo.com)
Date: 10/15/03


Date: Wed, 15 Oct 2003 00:58:45 GMT

Hi,

Is it possible that class A references class B, and class B references class
A?

Specifically, here is my problem. I have a card table class (Table), which
represents
a table at which players might play blackjack or some other game. I also
have a Hand
class, which represents the various hands being played at the table. Thus
Table has
a member which is a list of Hand objects. However, it will sometimes be
convenient for
a Hand object to have a reference to the Table which contains it: eg, a
blackjack hand
might need to split itself, which would involve adding a new Hand to the
Table's list
of hands. So I thought I could make a Table& one of the members of Hand.
But now
the compiler has a chicken and egg problem: It has to compile one class
first, but whichever
class it does try to compile first will reference the other class, which is
not yet defined.

I think that either:

1. There is some general design pattern I should be using instead of
referencing a containing
object, and that this patter would solve my problem better.

2. There is some way to make this work.

Does anyone know?

Thanks,
cpp

In case the above description isn't clear enough, here is my (impossible to
compile) code:

#include "stdafx.h"
#include <string>
#include <algorithm>
#include <list>
using namespace std;

class Table {
private:
 list<Hand> m_hands;
public:
 void addHand(Hand h) {m_hands.push_back(h);}
 void showHands() {
  list<Hand>::iterator iter;
  for (iter = m_hands.begin(); iter != m_hands.end(); iter++)
   *iter.show();
 }
};

class Hand {
private:
 int m_card1;
 int m_card2;
 Table& m_table;
public:
 void show() {cout << m_card1 << " " << m_card2 << endl;}
 void setCards(int i) {m_card1 = i; m_card2 = i;}
 int getCard1() {return m_card1;}
 void split() {m_table.addHand(*this);}
};

int main() {
 Table myTable;
 Hand myHand;
 myHand.setCards(1);
 myTable.addHand(myHand);
 myHand.setCards(2);
 myTable.addHand(myHand);
 myHand.split();
 myTable.showHands();
 system("PAUSE");
 return 0;
}



Relevant Pages

  • Re: translation functions
    ... The Compile option turning gray is normal: ... Doug Steele, Microsoft Access MVP ... None of the references are missing ... then go back in and unselect the reference you just added. ...
    (microsoft.public.access.gettingstarted)
  • Re: Library Database
    ... in the same folder location as the front-end database when you ... set the reference and compile the front-end MDE, ... Two of the libraries also reference ... all of this by simply not using references, ...
    (comp.databases.ms-access)
  • Re: Names of textboxes on tab-pages within a tab control?
    ... Compact the database to get rid of this junk: ... Uncheck any references you do not need. ... Still in the code window, choose Compile from the Debug menu. ...
    (comp.databases.ms-access)
  • Re: VS2005 mixed project intellisense confusion
    ... No idea or suggestion but my advice always is to keep the two project ... references, not via the links: ... wrong selection) the next compile run for the active project (or any ... except the primary) fails as the IDE compiled the respective module with ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: VB program conversion from Windows 95 to Windows XP
    ... >>> compile error changed each time, ... >> Are there any missing references at all? ... If you post the contents of your VBP file, ... >> by opening the project file in Notepad. ...
    (microsoft.public.vb.general.discussion)