How can I prevent a base constructor from being called by an inheritor in C#?

There is a way to create an object without calling any instance constructors. Before you proceed, be very sure you want to do it this way. 99% of the time this is the wrong solution. This is how you do it: FormatterServices.GetUninitializedObject(typeof(MyClass)); Call it in place of the object’s constructor. It will create and return … Read more

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

Scope of variables in if statements

“Do variables declared in a conditional go out of scope at the end of the conditional?” Yes – the scope of a local variable only falls within enclosing brackets: { int x; //scope begins //… }//scope ends //x is not available here In your case, say you have class A. If you’re not dealing with … Read more

Ternary operator + C++11 constructor from initializer_list [duplicate]

Standard writes in 8.5.4.1: List-initialization Note: List-initialization can be used as the initializer in a variable definition (8.5) as the initializer in a new expression (5.3.4) in a return statement (6.6.3) as a function argument (5.2.2) as a subscript (5.2.1) as an argument to a constructor invocation (8.5, 5.2.3) as an initializer for a non-static … Read more

Why doesn’t the standard consider a template constructor as a copy constructor?

Let’s put templates aside for a second. If a class doesn’t declare a copy constructor, an implicitly defaulted one is generated. It may be defined as deleted, but it’s defaulted nonetheless. A member template is not a member function. Members are instantiated from it only when needed. So how can a compiler know from the … Read more