I can create a pointer in class A that can access classes B and C however is it possible for classes B and C to then look back at class A?

and is it possible for classes B and C to access each others variables and functions?

I have got around this problem in the past by just making the class a function in side the class it needs to share information with however this is making class A bigger then it should be and causing other sorts of problems, what I am trying to do maybe bad coding practice so any suggestions on a way around this problem is greatly welcome.

06-26-2005

Gravedigga

To make let's say class B to have access to members of class C you just do that:

Code:

class C{
...
friend class B;
//you can also make just one function to have access to class B:
friend MyFunc(....);
};

If you want classes B and C to turn back to A you must declare a pointer to an object of class A like this:

Code:

class A;//this is smth like a declaration of a function
class B{
A *myPointer;
//.....
};

Sorry if my English is bad.

06-26-2005

xErath

B and C can never look at the A instance which contains them, because It impossible to know if they're even contained wherelse. The solution would be to add a friend method to B and C which receives a instance of A

06-26-2005

Gravedigga

They can look at the instance of A, which contains them if they have a poiter of type A and in (for example) the constructor of A somehow we make this pointers to "point" to "this" (which is the object of type A).

06-26-2005

Heavens

Ok can someone post some code to show me what you mean as i cant get any of it to work each class has its own cpp and h file if tat makes a differance thanks

I have created the example in the editor of replies, so it may contain syntax errors, but I think the may idea is clear.

Wish me luck- Tomorrow I have a test on Complex Analisis. :(

06-26-2005

Zach L.

I generally try to avoid this scenario. Instead, I prefer to create a 'Link' object between two objects (of different types), with some mechanisms for querying and sending messages between the two 'ends' of the link.

From my perspective, this makes for a much cleaner relationship between objects.

06-26-2005

7stud

When two classes have pointers to each other, how do you create objects of the classes?

06-27-2005

sigfriedmcwild

You create them as normal
Then at some point you set the 2 pointers (before using them of course).

In this scenario you would create object A, then B (most likely within A's constructor). At this point A has a pointer to B (or has the entire instance of B) now A can pass B its this pointer so B can set its pointer to A (this can be done in B's constructor).

In a more general case each object has a method to set the pointer ot the other and they are called after object creation (possibly by a third object that knows about both)

Remember that a pointer is not an instance of a class. A pointer is a varialbe that holds the location to an instance of a class. This mean that a pointer might not "point" to any instance at all. This what makes it possible to create 2 objects that have poitners to each other.

06-28-2005

Heavens

I still cant get classes and B and C to look at each other data does someone have a code example of this using different files for each class?