delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/03/06/00:38:06

Date: Thu, 6 Mar 1997 00:35:47 -0500 (EST)
From: Michael Phelps <morphine AT hops DOT cs DOT jhu DOT edu>
To: Graham Howard Wile <an118 AT chebucto DOT ns DOT ca>
cc: djgpp AT delorie DOT com
Subject: Re: HELP - need help resolving conundrum with classes
In-Reply-To: <5fl6a5$4ic@News.Dal.Ca>
Message-ID: <Pine.GSO.3.95.970306003414.21399E-100000@hops.cs.jhu.edu>
MIME-Version: 1.0

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:

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019