Mail Archives: djgpp/1997/03/06/08:39:17
Date: | Thu, 6 Mar 1997 07:13:53 -0600 (CST)
|
From: | Andrew Deren <aderen AT eecs DOT uic DOT edu>
|
To: | djgpp <djgpp AT delorie DOT com>
|
Subject: | Re: HELP - need help resolving conundrum with classes
|
In-Reply-To: | <Pine.GSO.3.95.970306003414.21399E-100000@hops.cs.jhu.edu>
|
Message-ID: | <Pine.SUN.3.95.970306071047.12072C-100000@ernie.eecs.uic.edu>
|
MIME-Version: | 1.0
|
On Thu, 6 Mar 1997, Michael Phelps wrote:
> On 6 Mar 1997, Graham Howard Wile wrote:
>
> >
> > 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:
> >
>
> Put a prototype of the class(es) you will be referring to _before_ the
> class declaration, to inform it that it is a class and will be described
> later. In your example:
>
I am not sure if you can do this, because it would lead to a recursive
calling of the class. Let's say in main you would declare
main()
{
the_owner master;
}
then when this would be created master would have the_cats and each of
those cats would have the_owner and the_owners would have the_cats...
Instead it would be better to use pointers instead of arrays.
I am not sure. Correct me if I am wrong.
> class the_owner;
>
> >
> >
> > 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
> >
> >
> >
> > --
> >
>
>
> ---Michael Phelps
> morphine AT cs DOT jhu DOT edu
>
>
> CH3
> |
> N
> / |
> ______/ |
> / \ CH2
> _____/ \__|__
> // \\ / | \\
> // \\______/___CH2 \\
> \ / \ /
> \______/ \_____/
> / ------ \ / \
> OH \ / OH
> O
>
> Morphine
>
>
>
>
- Raw text -