How do I initialize a const data member?

The const variable specifies whether a variable is modifiable or not. The constant value assigned will be used each time the variable is referenced. The value assigned cannot be modified during program execution. Bjarne Stroustrup’s explanation sums it up briefly: A class is typically declared in a header file and a header file is typically … Read more

C++ Member Initializer List

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 happens if a constructor throws an exception?

No, throwing an exception is the best way to signal an error during object construction. (Since there’s no return value, there’s no other way, other than constructing a headless object, which is bad style in C++.) From the man himself, Bjarne Stroustrup: http://www.stroustrup.com/bs_faq2.html#ctor-exceptions (If you are working in a project where exceptions aren’t allowed, then … Read more

Initialization difference with or without curly braces in C++

Short version Initialization via {..} is list-initialization, which prohibits narrowing conversions. For example, if LLONG_MAX is the maximum value of an long long int, and your int cannot represent that: int x = LLONG_MAX; // probably accepted with a warning int x {LLONG_MAX}; // error Similarly: long long y = /*something*/; int x = y; … Read more

Why can’t I refer to an instance method while explicitly invoking a constructor?

Non-static methods are instance methods. This are only accessible in existing instance, and instance does not exist yet when you are in constructor (it is still under construction). Why it is so? Because instance methods can access instance (non-static) fields, which can have different values in different instances, so it doesn’t make sense to call … Read more

iOS Designated Initializers : Using NS_DESIGNATED_INITIALIZER

The use of NS_DESIGNATED_INITIALIZER is nicely explained in http://useyourloaf.com/blog/2014/08/19/xcode-6-objective-c-modernization.html: The designated initializer guarantees the object is fully initialised by sending an initialization message to the superclass. The implementation detail becomes important to a user of the class when they subclass it. The rules for designated initializers in detail: A designated initializer must call (via super) … Read more

C++ – value of uninitialized vector

The zero initialization is specified in the standard as default zero initialization/value initialization for builtin types, primarily to support just this type of case in template use. Note that this behavior is different from a local variable such as int x; which leaves the value uninitialized (as in the C language that behavior is inherited … Read more