Mail Archives: djgpp/1997/03/05/22:00:27
From: | an118 AT chebucto DOT ns DOT ca (Graham Howard Wile)
|
Newsgroups: | comp.os.msdos.djgpp
|
Subject: | HELP - need help resolving conundrum with classes
|
Date: | 6 Mar 1997 01:23:17 GMT
|
Organization: | Chebucto Community Net
|
Lines: | 70
|
Message-ID: | <5fl6a5$4ic@News.Dal.Ca>
|
NNTP-Posting-Host: | chebucto.ns.ca
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
How can I declare two different classes, where each class has a member
variable of the other class type. My problem is how to get the compiler to
recognize a variable type before it has been created.
EXAMPLE:
class the_cat
{
private:
int its_age, its_weight;
char *fur_color;
the_owner its_owners[3]; //each cat has 3 owners, or people who
//look after it
//I know this is illegal, because the
//class "the_owner" hasn't been
//declared yet
public:
//whatever member functions the cat might have
};
class the_owner
{
private:
the_cat cats_owned[5] //each owner owns 5 cats
car *owner_occupation //what owner does for a living
public:
//whatever member functions the owner might have
};
Naturally, this won't work. The class "the_cat" can't declare a
variable of type "the_owner" if the class "the_owner" hasn't been created yet.
BUT - I can't reverse the class declarations and have "the_owner"
declared first, or else "the_owner" would not be able to declare any variables
of type "the_cat"
Alas, the order of the class declarations shown above will allow me to
have an owner that has 5 cats, but none of the cats can have an owner. In the
other order, I can produce a cat with 3 owners, but none of the owners will be
able to have any cats!
Does anyone know how I can declare these two classes so that an owner
can have cats AND the cats have owners too ? I don't need a real long answer
that spells the whole thing out for me. I just need to know what
keywords/concepts I can look up that would likely address this conundrum.
Thanks,
Graham
--
- Raw text -