Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Do initialized class member from deceleration initialized again in the constructor if it's in initializer list?

I have this class:

class A 
{
    bool isSomthing{true};
    A() : isSomthing{isSomthingFunc()}{};
    bool isSomthingFunc() {return isSomthing && isSomthingFuncExt()};
};

Can someone please explain what is going to be the value of isSomething if isSomthingFuncExt() returns false (what is going to happen in case isSomthingFuncExt() return val is constexpr and what if not)?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your code invokes undefined behavior.

The example seems to be made up to illustrate some wrong and very round about understanding of how initializers are used. However, it is actually rather simple: The default initializer is only used when there is no initializer in the member initializer list. Hence, in your example the default initializer is not used in the constructor.

The constructor

A() : isSomthing{isSomthingFunc()}{};

uses isSomethingFunc() to initizliaze the member. It does NOT use the in-class default true initializer.

You could write for the same effect:

class A 
{
    bool isSomthing;  // no default initializer !!
    A() : isSomthing{isSomthingFunc()}{};
    bool isSomthingFunc() {
        return isSomthing &&          // UB: reading before initialization
               isSomthingFuncExt()
    };
};

Calling isSomthingFunc invokes undefined behavior, because it reads from the member before it has been initialized.

For details I refer you to: https://en.cppreference.com/w/cpp/language/constructor ("If a non-static data member has a default member initializer and also appears in a member initializer list, then the member initializer is used and the default member initializer is ignored:").

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading