I have code like this for example :
class A {
public:
int x;
A() {
std::cout << "Constructor Called !" << std::endl;
}
A(int y):x(y) {
std::cout << "Constructor Param Called !" << std::endl;
}
A(const A& copy) {
std::cout << "Copy Constructor Called !" << std::endl;
}
}
class B {
public:
A value;
//B(const A& val) : value(val){}
}
int main(){
B b { A(22)};
}
If i comment out the B constructor the output is just "Constructor Param Called", but if i uncomment B constructor the output would be "Constructor Param Called" & "Copy Constructor Called". My questions :
- Why is the output different if i commented out the constructor ? (I've read about aggregate class & aggregate initialization, is this it ?)
- What's the difference between aggregate initialization & direct initialization ?
Adefault constructor and copy constructor are not initializing thexmember, so in the cases where aBobject is default-constructed without anAinput value, or if aBobject is constructed with anAinput value and theBconstructor is uncommented, then the value ofB.value.xwill be indeterminate after construction, which will lead to undefined behavior ifB.value.xis ever read from.