Quantcast
Channel: QtWebEngine
Viewing all articles
Browse latest Browse all 13965

Access class member from another class only

$
0
0
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.

Viewing all articles
Browse latest Browse all 13965

Latest Images

Trending Articles



Latest Images