Is a member initializer list part of the declaration or the definition of a constructor?

Just to clarify something that came up in some of the other answers… There is no requirement that the initializer list be in either the source (.cpp) or header (.h) file. In fact, the compiler does not distinguish between the two types of files. The important distinction is between the contructor’s declaration and it’s definition. … Read more

What is the difference between initialization and assignment in a constructor?

What might go wrong if we perform assignment instead of initialization? Some class types (and also references and const objects) can’t be assigned; some can’t be default-initialised; some might be more expensive to default-initialise and reassign than to initialise directly. Doesn’t the compiler internally performs assignment in case of test1() constructor? If no then how … Read more

Can member functions be used to initialize data members in the member initializer list?

Yes, your use of member function in initialization list is valid and complies with the standard. Data members are initialized in the order of their declaration (and that’s the reason why they should appear in the initialization list in the order of their declaration – the rule that you followed in your example). N_ is … Read more

How to initialize an array member in a member initializer list

How can I do what I want to do (that is, initialize an array in a constructor (not assigning elements in the body)). Is it even possible? Yes. It’s using a struct that contains an array. You say you already know about that, but then I don’t understand the question. That way, you do initialize … Read more

What does a colon following a C++ constructor name do? [duplicate]

This is a member initializer list, and is part of the constructor’s implementation. The constructor’s signature is: MyClass(); This means that the constructor can be called with no parameters. This makes it a default constructor, i.e., one which will be called by default when you write MyClass someObject;. The part : m_classID(-1), m_userdata(0) is called … Read more

Initializing a member array in constructor initializer

How can I do what I want to do (that is, initialize an array in a constructor (not assigning elements in the body)). Is it even possible? Yes. It’s using a struct that contains an array. You say you already know about that, but then I don’t understand the question. That way, you do initialize … Read more

Member initialization while using delegated constructor

When you delegate the member initialization to another constructor, there is an assumption that the other constructor initializes the object completely, including all members (i.e. including the lines member in your example). You can’t therefore initialize any of the members again. The relevant quote from the Standard is (emphasis mine): (ยง12.6.2/6) A mem-initializer-list can delegate … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)