I would no call it a "paradigm"; this is just a tiny syntactic feature. It looks like StackOverflow already gave you an answer; please check it.
I need to add something to this answer. First, instance (non-static) fields are recommended to be initialized in C++ initialization statement, which applies only to constructors and is important fool-proof syntactic feature. See how
x(0)
initializes
x
:
class InitDemo {
static int y;
int x;
public:
InitDemo() : x(0) { }
};
int InitDemo::y = 10;
The problem is initialization of a static field. Of course, you could initialize it in the constructor, but the result of it would not be what you want. Consider:
class InitDemo {
static int y;
int x;
public:
InitDemo() : x(0) {
y = 10; }
};
It works at least once, but imagine what happens if you want to accumulate some value across all the instances of this class. In this case, each constructor would reset the modified value of
y
to 10, effectively cancelling the result of previous calculations.
So, what do you think? Looks ugly compared to Java? And in .NET, there are very handy static constructors perfect for the purpose. I personally consider C++ as a ridiculously archaic language (despite its apparent success), but there are too many C++ fans ready to scratch out my eyes for such words… I'm prepared for down-votes… :-)
—SA