2

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 :

  1. Why is the output different if i commented out the constructor ? (I've read about aggregate class & aggregate initialization, is this it ?)
  2. What's the difference between aggregate initialization & direct initialization ?
2
  • 2
    On a side note: your A default constructor and copy constructor are not initializing the x member, so in the cases where a B object is default-constructed without an A input value, or if a B object is constructed with an A input value and the B constructor is uncommented, then the value of B.value.x will be indeterminate after construction, which will lead to undefined behavior if B.value.x is ever read from. Commented Jan 3, 2023 at 22:46
  • @RemyLebeau Oh you're right, at first I thought it would just initialize to 0 by default or something. Thanks for the info. Commented Jan 4, 2023 at 5:48

1 Answer 1

6

When you remove the user-provided constructor for B, B becomes an aggregate. So aggregate-initialization is performed where each element of the class is copy-initialized from the elements of the initializer list. Since A(22) is a prvalue of the same class as B's element, copy-elision takes place where the value is stored directly into the object without any calls to the copy-constructor. This is new as of C++17.

When you declare the constructor for B, it is no longer an aggregate, so constructors are considered when you're doing an initialization.

Direct-intialization just means there is no = sign when you're initializing an object. Aggregate-initialization is what takes place when you're initializing an aggregate, and you can take a look at the definition on cppreference for that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.