Order of calling base class constructor from derived class initialization list

The order you refer in your question is not the “order of calling base constructor”. In fact, you can’t call a constructor. Constructors are not callable by the user. Only compiler can call constructors. What you can do is to specify initializers. In this case (constructor initializer list) you are specifying initializers for subobjects of … Read more

Can member variables be used to initialize other members in an initialization list?

This is well-defined and portable,1 but it’s potentially error-prone. Members are initialized in the order they’re declared in the class body, not the order they’re listed in the initialization list. So if you change the class body, this code may silently fail (although many compilers will spot this and emit a warning). 1. From [class.base.init] … 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

Initialize parent’s protected members with initialization list (C++)

It is not possible in the way you describe. You’ll have to add a constructor (could be protected) to the base class to forward it along. Something like: class Parent { protected: Parent( const std::string& something ) : something( something ) {} std::string something; } class Child : public Parent { private: Child() : Parent(“Hello, … Read more

Automatically initialize instance variables?

You can use a decorator: from functools import wraps import inspect def initializer(func): “”” Automatically assigns the parameters. >>> class process: … @initializer … def __init__(self, cmd, reachable=False, user=”root”): … pass >>> p = process(‘halt’, True) >>> p.cmd, p.reachable, p.user (‘halt’, True, ‘root’) “”” names, varargs, keywords, defaults = inspect.getargspec(func) @wraps(func) def wrapper(self, *args, **kargs): … Read more

Benefits of Initialization lists

The second version is calling string’s default ctor and then string’s copy-assignment operator — there could definitely be (minor) efficiency losses compared to the first one, which directly calls c’s copy-ctor (e.g., depending on string’s implementation, there might be useless allocation-then-release of some tiny structure). Why not just always use the right way?-)

In this specific case, is there a difference between using a member initializer list and assigning values in a constructor?

You need to use initialization list to initialize constant members,references and base class When you need to initialize constant member, references and pass parameters to base class constructors, as mentioned in comments, you need to use initialization list. struct aa { int i; const int ci; // constant member aa() : i(0) {} // will … Read more