-3

Suppose we have a class A:

class A{
    int a_;
public:
    friend class B;
    A(int a):a_(a){}
    int getA(){ return a_;}
    void setA(int a){a_ = a;}
    void print(int x){cout << x << endl;}
};

and another class B:

class B{
    int b_;
public:
    B(int b):b_(b){}
    void setB(int b){b_ = b;}
    int getB(){return b_;}
    //friend void A::print(int x);
};

How to use a method of class A like print() using an object of class B?

//main.cpp
B b1(10);
b1.print(b1.getB());
Jason
  • 36,170
  • 5
  • 26
  • 60
Krant
  • 23
  • 4
  • Why isn't `A::print` a static or free function? – Artyer Oct 22 '22 at 07:33
  • `A::print()` is publicly available anyways, why do you need `friend` for this case? – πάντα ῥεῖ Oct 22 '22 at 07:34
  • `b1.print(...` ... `B` doesn't have a `print` member function so why did you expect that to work? – Ted Lyngmo Oct 22 '22 at 07:34
  • *"How to use a method of class A like print() using an object of class B?"* **`B` isn't related to `A` in any way, so you can't**. You need an `A` object to use the `A::print` in your example. This is explained in any [begginer c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Jason Oct 22 '22 at 07:35
  • @Ted Lyngmo, I guessed that a friend class could access the member functions to the host class. – Krant Oct 22 '22 at 07:57

2 Answers2

1

Short answer: No, a friend class's object cannot access the methods of the class of which it is a friend. It can only access the data members(private or protected).

To access the members of the class with which a class is a friend, use the object of the appropriate class.

Suppose, class B is a friend to class A, to access the members of the class A, always use the objects of the class A. In no case, an object of class B can be used to access the members of class A, be it private, public or protected.

Krant
  • 23
  • 4
0

How to use a method of class A like print() using an object of class B?

B isn't related to A in any way so you can't. The only thing you've done by adding a friend declaration is that you've allowed class B(or B's member functions) to access private parts of class A through an A object. Note the last part of the previous sentence. We still need an A object to be able to call A::print().

That is, friendship doesn't mean that you can directly(without any A object) access A's private members.

Jason
  • 36,170
  • 5
  • 26
  • 60
  • Thanks @Jason Liam! I was looking for a way to access a class methods using a different class object, but it seems to be impossible in C++. – Krant Oct 22 '22 at 07:54
  • @Krant Yes, it is not possible as the classes are completely unrelated. They're not even related by inheritance. You're welcome :) – Jason Oct 22 '22 at 07:55
  • To correct my previous comment, it is not impossible to access a class member using an object of a different class. We can do so if the classes are related through inheritance. – Krant Oct 23 '22 at 04:05
  • 1
    @Krant Note that even if they're related through inheritance, we can only access the base class' member of the base class' subobject in the derived class. It is just not possible to access directly another class member using an object of another class even if they're related by inheritance. What happens(when they're related through inheritance) behind the scenes is that the base class' subobject's member is used. – Jason Oct 23 '22 at 05:01
  • thanks for further clarification, your comments are quite valuable! – Krant Oct 25 '22 at 06:52