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;
} 4 8883
"cppaddict" <cp*******@yahoo.com> wrote in message
news:9Z***************@newssvr29.news.prodigy.com. .. 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.
Yes, this is an interesting problem. Technically the hands belong to the
casino, and players can't touch them. On the other hand, players are the
only ones who can make decisions on their hands - the casino can't.
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.
Use a pointer to the Table from the hand, not a reference. That way you can
define a forward reference. In general, it looks like this:
class A;
class B
{
A* ptrToA;
};
class A
{
B b;
};
cppaddict <cp*******@yahoo.com> wrote in message
news:9Z***************@newssvr29.news.prodigy.com. .. 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>
You don't need <string> or <algorithm>.
#include <list>
You do need <iostream>.
using namespace std;
class Table {
You have to put this class after the definition of class Hand.
private: list<Hand> m_hands; public: void addHand(Hand h) {m_hands.push_back(h);}
This would be better as:
void addHand(const Hand& h) {m_hands.push_back(h);}
void showHands() {
Better as:
void showHands() const
list<Hand>::iterator iter;
list<Hand>::const_iterator iter;
for (iter = m_hands.begin(); iter != m_hands.end(); iter++) *iter.show();
*iter.show() is wrong because the . has higher precedence than the *. Use
iter->show(). (If you really want to use the * and . then it needs to be
(*iter).show())
} };
After moving this above class Table, use this forward declaration:
class Table;
class Hand { private: int m_card1; int m_card2; Table& m_table;
You need a constructor to initiatialize this reference member.
public: void show() {cout << m_card1 << " " << m_card2 << endl;}
void show() const
void setCards(int i) {m_card1 = i; m_card2 = i;} int getCard1() {return m_card1;}
int getCard1() const
void split() {m_table.addHand(*this);}
This member will have to move.
};
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; }
Here is the complete, corrected and rearranged code:
#include <list>
#include <iostream>
using namespace std;
class Table;
class Hand {
private:
int m_card1;
int m_card2;
Table& m_table;
public:
Hand(Table &table) : m_table(table) {}
void show() const {cout << m_card1 << " " << m_card2 << endl;}
void setCards(int i) {m_card1 = i; m_card2 = i;}
int getCard1() const {return m_card1;}
void split();
};
class Table {
private:
list<Hand> m_hands;
public:
void addHand(const Hand& h) {m_hands.push_back(h);}
void showHands() const {
list<Hand>::const_iterator iter;
for (iter = m_hands.begin(); iter != m_hands.end(); iter++)
iter->show();
}
};
inline void Hand::split() {m_table.addHand(*this);}
int main() {
Table myTable;
Hand myHand(myTable);
myHand.setCards(1);
myTable.addHand(myHand);
myHand.setCards(2);
myTable.addHand(myHand);
myHand.split();
myTable.showHands();
system("PAUSE");
return 0;
}
David,
Thank you so much. That was *very* helpful.
Best,
cpp
"jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net... Use a pointer to the Table from the hand, not a reference.
Eyes saw "reference", brain read "value". This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Chuck Ritzke |
last post by:
I keep asking myself this question as I write class modules. What's the
best/smartest/most efficient way to send a large object back and forth to a
class module?
For example, say I have a data...
|
by: Jim Red |
last post by:
hello
first of all, i know, there are no classes in javascript. but i will use
that word for better understanding of my question.
here we go. i have three classes and need a reference to the...
|
by: Chris |
last post by:
Hi,
I don't get the difference between a struct and a class !
ok, I know that a struct is a value type, the other a reference type, I
understand the technical differences between both, but...
|
by: thomson |
last post by:
Hi all,
Would you please explain me where will be the heap stored if it
is declared inside the Class, As class is a reference type, so it gets
stored on the heap, but struct is a value...
|
by: Liz |
last post by:
ok, this is really simple stuff, or it should be ... but I'm stuck
In a Windows Forms app, I have something resembling this:
Form1.cs
========
namespace NS
Class Form1
|
by: Craig Buchanan |
last post by:
If I declare a class within another class like:
Class ParentClass
...
Class ChildClass
...
End Class
End Class
How do I reference a property in the ParentClass in the ChildClass? If the
|
by: Brett Romero |
last post by:
I have this situation:
myEXE <needs< DerivedClass <which needs< BaseClass
Meaning, myEXE is using a type defined in DerivedClass, which inherits
from BaseClass. I include a reference to...
|
by: Mike |
last post by:
Hi,
I have a form with some controls, and a different class that needs to
modify some control properties at run time.
Hoy can I reference the from so I have access to its controls and...
|
by: tshad |
last post by:
Using VS 2003, I am trying to take a class that I created to create new
variable types to handle nulls and track changes to standard variable types.
This is for use with database variables. This...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |