class stinker {
private:
int *pointer;
int age;
public:
stinker() : pointer(), age() { ;}
};

if i don't pass a value to my variables in an initialization list, do they automatically get initialized to 0?

Why don't you write a program and see for yourself?

yes

yes, but I would still recommend making the initial value explicit (as in stinker() : pointer(NULL), age(0) { }; . You are basically asking the person who reads your code to be aware of "value-initialization of POD type means zero-initialization". This is not so common (many people wouldn't even know the difference between default-initialization and value-initialization). This SO thread might clarify things for you.

if i don't pass a value to my variables in an initialization list, do they automatically get initialized to 0?

What happens is that the default constructor for the object is invoked (which in this case happens to be 0). Try doing it with an object that doesnt have a default constructor and you will see what I mean.

What happens is that the default constructor for the object is invoked (which in this case happens to be 0). Try doing it with an object that doesnt have a default constructor and you will see what I mean.

@L7Sqr: I'm sorry, but I have to correct your statement. I think it is an important point to clarify. Primitive types such as int or int* do not have a default constructor (or, equivalently, their "imaginary" default constructor does not initialize the memory in any way). However, there is the concept of default-initialization in C++ which is invoked with the following syntax:

int* b = new int(); //the int to which b points to is default-initialized.
struct Foo {
  int value;
  Foo() : value() { }; //the "value" member is default-initialized. 
};

When this default-initialization is used one of the following happens (C++ Standard 8.5/5):

To default-initialize an object of type T means:

— if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

— if T is an array type, each element is default-initialized;

— otherwise, the object is zero-initialized.

The standard also clearly states at 8.5/9 that, if no initializer is specified for an object of POD-type (which includes primitive types), then the object and any of its sub-objects have "indeterminate initial value".

Basically this means that the following this true (and can be tested to be true):

class Foo {
  int value;
  public:
    Foo() : value() { /* here, value == 0 */ }; 
};
class Bar {
  int value;
  public:
    Bar() { /* here, "value" can have any initial value (completely arbitrary) */ };
};

So, data members that are non-POD types, even if omitted from the initialization list, will have their default-constructor called. However, POD types (and primitive types), if they are omitted from the initialization list, will not be initialized at all. While if they are part of the initialization list with (), their memory will be set to zero (zero-initialized). The fact remains, there is no default constructor being called for POD types, just a simple initialization of the memory to zero (in fact the current standard requires that there be no constructors defined and the coming standard will require that only the trivial constructors exist).

The above might seem like a trivial point (as in: what's the difference between default-initialization and default construction?). But it is important to notice that this case of a POD data member being part of the initialization list with empty brackets is not an invocation of the default constructor. This could mislead someone into not understanding that int a; (which is analogous to MyClass a; ) does not initialize the memory because it does not call a default constructor because there is no default constructor for "int" or any other POD type (as opposed to the "MyClass" case which, if MyClass is non-POD, would invoke the default constructor of MyClass).

Finally, the lesson to be learned from all the above weird confusion is: if you want an initial value for a variable, give it an initial value explicitely, even if it is zero. That will make everyones life easier in the end.

commented: Thanks for the clarification. +1
commented: Good information +7

You are basically asking the person who reads your code to be aware of "value-initialization of POD type means zero-initialization". This is not so common (many people wouldn't even know the difference between default-initialization and value-initialization).

I would expect that anyone who has used a standard library container like std::vector<> would be aware of what value-initialization means. (However, it is still a good idea to make the initialization explicit.)

Incidentally, a non-POD can also be value initialized if it does not have a (has a trivial) constructor. eg.

struct not_pod
{
    ~not_pod() {}
    
    private: int a[5] ;
};

>>Incidentally, a non-POD can also be value initialized if it does not have a (has a trivial) constructor. eg.

That's true, I guess you managed to pick the one example that falls into the category (8.5/5):

— if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;

But I guess your example would fall under POD when the new standard comes in.

The point still remains, it is not recommended to create variables without giving them an explicit initial value, mostly because for good form, in parts because the rules that govern whether or not an "uninitialized" variable will be zero or not are not trivial to understand and can easily change for a small change in the content of a class declaration.

But I guess your example would fall under POD when the new standard comes in.

Not too sure about that mike. AFAIK, this is how it stands as of now:

9.5 A trivially copyable class is a class that:
- ...<elided>
- has a trivial destructor

12.4 A destructor is trivial if it is neither user-provided nor deleted and if: ...<elided>

9.5 A trivial class is a class that has a trivial default constructor and is trivially copyable.

9.9 A POD struct is a class that is both a trivial class and a standard-layout class, and has ... <elided>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.