Posted by Ricky Castles on April 15, 2001 at 00:35:23:
In Reply to: Bi-directional association posted by Brian Begnoche on April 14, 2001 at 22:29:04:
: When you have two classes, say a base class and a derived class, that must know about each other, do you use a forward declaration with the specification of just one class or both of them? I'm having a lot of trouble trying to figure out where to use a forward declaration, where to use an include, and where to have nothing. I'm specifically talking about four files: the header and source files for a base class and the header and source files for a derived class.
If you are strictly talking about base and derived classes, I don't believe you need to worry about forward declarations as the inheritance relationship takes care of it. The derived class will be able to use the public functions of the base class through public inheritance. The use of bi-directional association comes in when you are trying to associate an object of one class to another class and make sure that both know about each other. As in say you want each creature to know which manager governs it. This is the problem I am trying to figure out. I don't know if that helped you at all, I am having problems with the Bi-directional association too.
This is what I am trying to figure out:
So as to not give away my design, I will use generic names to describe my problem. I have a class Foo1 which has 2 lists (from the STL list). The lists are both of pointers to type Foo2. (Foo2 is a base class for several derived classes) I wish to contain a pointer in each object of type Foo2 to the object of type Foo1 which they are a part of/associated with. How does one go about telling the objects of type Foo2 which Foo1 they are associated with? I have seen an example in the Deitel & Deitel C++ how to program book (p. 451 for those of you that have that one) of using a forward declaration, but they use a new object to instantiate the pointer in line 32 and I would like a pointer to a specific object. Any ideas???? Thanks a lot.
Post a Followup