0

I have question on C++ behavior when initializing structures using a list. For example, the following code behaves the same in C and C++. The list initializes x:

struct X {
    int x;
};

int main(int argc, char *argv[])
{
    struct X xx = {0};    
    return 0;
}

Now, if I add a constructor, I find out through testing that the constructor is called instead of the simple initialization of the x member:

#include <iostream>
using namespace std;

struct X {
    int x;
    X(int);
};

X::X(int i)
{
    cout << "X(int i)" << endl;
    x = i;
}

int main(int argc, char *argv[])
{
    struct X xx = {0};

    return 0;
}

Output:

X(int i)

Is the behavior of C++ with an identical constructor (as above) to override the simple list initialization? Giving my testing that is what appears to happen.

Thanks!

2
  • This code does not reveal what would happen if you called the constructor, nor what the value of xx.x is. Google c++ constructor and see how to call a constructor. If it was a class not a struct, the default is private so such initialization/assignment is impossible. Commented Jun 7, 2020 at 21:16
  • @DavidG.Pickett the constructor was called as you can see in the output. Not sure what you are saying Commented Jun 7, 2020 at 21:21

1 Answer 1

3

The following syntax:

X xx = {0};

is just a form of copy-list initialization. This has the effect of invoking the constructor of X, as you observed.

The name of this initialization comes from the fact that this looks like a list is being copied, but just the regular constructor is invoked. Note that this will only consider implicit constructors.

Also the elaborated-type-specifier struct is not necessary in c++, in the declaration of xx.

Note that if you don't provide a constructor such as X(int) (as you did in the first example), then the declaration of xx will instead do aggregate initialization.

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

7 Comments

So is the default for copy list initialization, without a constructor specified, to initialize the members of the struct in order?
Even without a user-provided constructor, the compiler will provide a constructor for you. Since the behavior of the default constructor is member wise initialization. yes.
"This has the effect of invoking the constructor of X" - only if the class has a user-provided constructor. Otherwise it is aggregate initialization and no constructor is called
@cigien If you don't declare X::X(int) yourself, I don't think such a constructor is declared or defined for you. Instead, if no constructors are user-declared, X becomes an aggregate class, and the syntax X xx = {5}; becomes aggregate initialization. You can see there is no such constructor by trying: X x(5);. This fails, because there is no constructor to call. In C++20, this will work, but again, that's because it becomes aggregate initialization, which bypasses constructors.
@M.M So "class type with default constructor", specifically means user-provided default constructor?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.