Search code examples
c++friend

Using constructor of Friend Class


I have two classes A and B. A has declared B as a friend. In B I would like to instantiate A in method func() (i.e I am trying to instantiate A outside of the constructor of B). To my suprise this seems to be not allowed in C++. This is the code:

class A {
public:
        friend class B;
        A(int x, int y) {
                x_ = x;
                y_ = y;
        }
protected:
        int x_, y_;
};

class B {
public: 
        B (int z) {
                z_ = z;
        }

        void func () {
                a (3,4);
        }
protected:
        A a;
        int z_
};

I get the following errors:

friendConstructor.cpp: In constructor ‘B::B(int)’:
friendConstructor.cpp:14:12: error: no matching function for call to ‘A::A()’
friendConstructor.cpp:14:12: note: candidates are:
friendConstructor.cpp:4:2: note: A::A(int, int)
friendConstructor.cpp:4:2: note:   candidate expects 2 arguments, 0 provided
friendConstructor.cpp:1:7: note: A::A(const A&)
friendConstructor.cpp:1:7: note:   candidate expects 1 argument, 0 provided
friendConstructor.cpp: In member function ‘void B::func()’:
friendConstructor.cpp:19:9: error: no match for call to ‘(A) (int, int)’

I have a situation where I can't instansiate class A in class B's constructor. I have to wait for something to happen before I instansiate class A. If what I am trying to do is impossible in C++. Can you suggest an alternative?


Solution

  • In func, a is a member variable, so it's instance was initialized (default constructed since you didn't specify how you wanted it initializing) when your instance of B was constructed. It has to be - it's a part of B.

    What you are actually doing is calling overloaded A::operator(). The "a(3,4)" syntax only means "construct with these arguments" in a declaration or an initializer list of a constructor.

    Your solutions are to add a member function to A to allow you to assign variables or construct a temporary and use assignment.

     a = A(3,4);