-
Class sharing
If i have three classes A, B and C
Code:
class A {
private:
B b;
C c;
};
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.
-
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.
-
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
-
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).
-
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
-
Code:
//in B.h
class A;
class B{
A *ContainingObject;
....
B();
void SetContaingObject(A *a);
void ShowContaingObject();
};
//in B.cpp
....
void B::SetContaingObject(A *a)
{
if(a!=NULL)
ContainingObject=a;
}
B::B(){
ContaingObject=NULL;//#define NULL 0x0
}
//in A.h
class A{
...
B obj;
};
//in A.cpp
A::A(){
obj.SetContainigObject(this);
........
}
So the object ot type B, which is in the object of type A (obj) can perform operation on the containing object:
Code:
//in B.cpp
void B::ShowContaingObject()
{
if(ContaingObject)
cout<<(void*) ContaingObject;
}
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. :(
-
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.
-
When two classes have pointers to each other, how do you create objects of the classes?
-
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.
-
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?