[solved] Access class member from another class only
-
I am writing two classes: MyClassA, MyClassB, none derived from the other, but MyClassB stores and uses objects of type MyClassA. I would like to expose both classes to the user, but I would like some methods of the MyClassA to only be used from MyClassB, but not from anywhere outside MyClassB. In code:
@class MyClassA{
public:
void FunctionA();
void FunctionB();/* public? private? anything else ?*/
void FunctionC(); // this method shall only be used from MyClassB!!!!
};class MyClassB{
private:
MyClassA A_object;public:
void MyOtherFunc(){
// I want to be able to call
A_object.FunctionC();
}
};int main(){
// but I don't want to be able to call
MyClassA object; // FunctionC from anywhere outside MyClassB
object.FunctionC();
}@When you don't want to expose a class memeber to the user you usually put it under the private or protected access types. But in my case private wouldn't let me use FunctionC from MyClassB(), while public would allow FunctionC to be used from any object outside MyClassB.
So how do I deal with this? From searches I understood the keyword friend might be a solution but I don't get how should I use it.
-
Hi,
friend keyword will be your best friend :)
For example
@class ClassA
{
friend class ClassB;
....private:
void func1();
};
@@
class ClassB
{
....public:
void aFunc() {member.func1();}
private:
ClassA member;
};
@Bu the question is: "Why do you expose MyClassB to user if you don't want he uses it directly? "
-
I have some back-end functions in MyClassA that I sometimes need to call from MyClassB as well, but I don't want to expose these functions to the user. Also, MyClassB would be using those functions as back-end functions as well, so they're not going to be exposed to the user from MyClassB; still, MyClassB will expose some other public functions to the user, so that's why I want to expose MyClassB.
Anyway, thank you for your help! I now understood what this friend was all about... in my tries I was using it the opposite (friending MyClassA from MyClassB...).
-
Could I suggest to create a new MyClassC accessible only to MyClassA and MyClassB?
-
Not quite since the back-end functions use objects inside the MyClassA and MyClassB classes; MyClassC would end up in a lot of unneeded data exchange functions to pass objects from a class to another... I thought about this solution too but I realized it's not as nice as it seems.
I'm redefining the object model right now to see if I can make it work properly without the friend approach. Chances though are that I'll end up having many parameters in functions... -
Hi,
it's ok.
Remeber however that using friend keyword you break "Object Oriented Encapsulation":http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)So, use carefully and think about different way to obtain what yuo want
-
thanks mcosta, I read about friend before and I already know it's out of the OOP concept. Now I figured out possible different way and I'm working on it :-)