Here are two classes
class A
{
std::string s;
public:
A() { prn("A constructor"); }
A(std::string s) : s(s) { prn("A not empty constructor"); }
A(const A&) { prn("A copy constructor"); }
A& operator =(const A& a) { prn("A = operator"); return *this; }
void p() { prn("in A"); }
};
class B
{
public:
A a;
B(A aa) : a(aa) { prn("B constructor"); }
B() { prn("B default constructor"); }
};
Now following code works correctly
B b(A("sa"));
b.a.p();
Prints:
A not empty constructor
A copy constructor
B constructor
in A
But if I use A constructor without parameters something strange happens
B b(A());
Compiles and run but no output (No constructors have been called)
B b(A());
b.a.p(); // error here
Got compile error. So whats the difference between these two constructors?