#include <iostream>
using namespace std;
class A;
class B {
public:
A createA();
};
class A {
private:
A() {
cout << "A Object created" << endl;
}
friend A B::createA();
};
A B::createA() {
A a_obj;
return a_obj;
}
int main(int argc, char* argv[]) {
B b;
A a = b.createA();
}
This code above works properly!
But I have the following questions:-
- I tried swapping the position of
class A
andclass B
but it produced an error saying thatA::A()
is inaccessible and trying to access incomplete typeclass B
Why? - Can anyone help me understand what is happening when you write
friend A B::createA();
is it trying to access the definition ofcreateA()
immediately.