How do I initialize a stl vector of objects who themselves have non-trivial constructors?

There are many ways to get there. Here are some of them (in no particular order of presence). Use vector(size_type n, const T& t) constructor. It initializes vector with n copies of t. For example: #include <vector> struct MyInt { int value; MyInt (int value) : value (value) {} }; struct MyStuff { std::vector<MyInt> values; …

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

Is std::move really needed on initialization list of constructor for heavy members passed by value?

My question: is this std::move really needed? My point is that compiler sees that this p_name is not used in the body of constructor, so, maybe, there is some rule to use move semantics for it by default? In general, when you want to turn an lvalue to an rvalue, then yes, you need a …

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

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

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

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