Okay, first off this is a trivial question, but nonetheless it's bugging me. Disclaimer: I'm not a compiler engineer. But it seems to me the compiler in this case is requiring a constructor that is really not necessary. Code below; why is the B constructor that takes no parameters and does nothing being called when an already instantiated object of it's class is being passed to the constructor of another class? For reference I'm using g++ (GCC) 5.3.0 and have not tried it with any other compilers (and yeah I know GCC is not without its quirks):
#include <iostream>
namespace I_DO_NOT_GET_IT
{
class A;
class B;
}
class B
{
public:
B()
{
std::cout << "\nWhy am I here?\n\n";
}
B(const int aInt, const char * aChar)
{
mInt = aInt;
mChar = aChar;
}
void identify()
{
std::cout << "\nI am an object of class B, owned by class A\n"
<< "my integer data is "
<< mInt
<< " and my character data is \""
<< mChar
<< "\"\n\n";
}
int mInt;
const char * mChar;
};
class A
{
public:
A(B an_instantiated_object_of_class_B)
{
b = an_instantiated_object_of_class_B;
}
// class A owns an object of class B
B b;
};
int main()
{
// create an object of class B
B b(1, "text");
// pass the B object to A, which uses it to instantiate its B object
// in the A constructor.
A a = B(b);
// Have A's B object describe itself
a.b.identify();
return 0;
}