Next: Templates
Up: C++/C
Previous: Converting from numbers to
Working with multiple classes that interact can cause some problems.
Here are some tips:
- To use each of two classes within the other, you need to declare the
second one before using it in the first:
class secondclass;
class firstclass{
secondclass* foo;
...}
class secondclass{
firstclass* foo2;
...}
- To pass a pointer to a object into another object, memory for the
object must already be allocated:
- //this doesn't work:
parm2* Z;
parm* a=new parm(Z);
Z=new parm2();
- //Instead, you need to pass the allocated location of Z to
a
parm* a=new parm();
parm2* Z=new parm2();
a->associate(Z); /* associate is a member function of parm
that makes use of Z; e.g. it could create a member object of 'a' that
points to *Z */
Chris Paciorek
2006-04-02