4

I have simple sample:

#include <iostream>

class parent {
public:
    int i;
};

class child : public parent {
public:
    int d;
};

int main() {
    child c;
    std::cout << c.d << std::endl;
    return 0;
}

If you do not explicitly initialize a base class or member that has constructors by calling a constructor, the compiler automatically initializes the base class or member with a default constructor.

but all ints in c (int d; and int i;) are not initialized.

enter image description here

What is wrong with it? Or I do not see something obvios?

myWallJSON
  • 9,110
  • 22
  • 78
  • 149
  • http://stackoverflow.com/questions/563221/is-there-an-implicit-default-constructor-in-c look at the first answer's section default constructors and the notes for PODs – Csq Jan 23 '13 at 22:23
  • 1
    Fundamental types don't have constructors. See http://stackoverflow.com/a/5113385/1801919. – Andrei Tita Jan 23 '13 at 22:23
  • The link that you provide is also for a Linux compiler, not VS2010 – Rob Goodwin Jan 23 '13 at 22:23
  • @RobGoodwin which link? which you? :) – Csq Jan 23 '13 at 22:25
  • @Csq the one in the question, right below the code block – Rob Goodwin Jan 23 '13 at 22:29
  • But, isn't a conceptual error say that the variable was not initialized instead of assigned? "Initialize" is once, "assignment" can be done many times. In my opinion the variable was initialized but not assigned... Microsoft should review this warning message =P – Lucas Marcondes Pavelski Jan 23 '13 at 22:29
  • 1
    @LucasMarcondesPavelski I think technically the variable is initialized, but not zero-initialized. – juanchopanza Jan 23 '13 at 22:48
  • 1
    @juanchopanza : Indeed, though pedantically the correct terms here are that the variables are _default-initialized_ but not _value-initialized_. – ildjarn Jan 23 '13 at 23:23
  • @ildjarn there-s a whole chain of *initializeds* for built-in types. *value-initialized* means *zero-initialized* in this case, so I thought I would cut out the middle man. – juanchopanza Jan 23 '13 at 23:25
  • @juanchopanza : Fair enough – as I said, I was being pedantic. :-] – ildjarn Jan 23 '13 at 23:38
  • As a side note, consider yourself lucky on two grounds. First, some compilers will zero-initialise your builtins for you, and you'd have never known you are doing something wrong until when it actually hurts you the most. Second, reading uninitialised values (your `cout` statement) is undefined behaviour which could have caused just about anything imaginable to happen. – Happy Green Kid Naps Jan 23 '13 at 23:43

3 Answers3

4

With built-in types, you actually have to do the initialization yourself:

class parent
{
 public:
  parent() : i() {}
  int i;
};

This will initialize i to 0.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
4

Built in data types (like int) are not really initialized. Their "default constructor" does nothing and they have no default value. Hence, they just get junk values. You have to explicitly initialize built in data types if you want them to have a specific value.

Cornstalks
  • 37,137
  • 18
  • 79
  • 144
  • so for example for Enums and all other class simple data types (and complex) I would have to do what [juanchopanza](http://stackoverflow.com/a/14490503/1056328) suggests? – myWallJSON Jan 23 '13 at 22:27
  • @myWallJSON: Yes. Except `std::complex` has a constructor that initializes its members to zero. If there isn't a constructor initializing something (or something explicitly being assigned to it), then it's going to have a junk value. – Cornstalks Jan 23 '13 at 22:35
3

There is a difference between default and zero initialization done on classes with no constructor and basic types:

child c1;           // Default initialized. int types are not initialized.
child c2 = child(); // Zero initialized.    int types are in initialized to zero.
// In C++ 11
child c3 {};        // Use new syntax for zero initialization

More detailed explanation:
here: https://stackoverflow.com/a/7546745/14065
here: https://stackoverflow.com/a/8280207/14065

Community
  • 1
  • 1
Martin York
  • 257,169
  • 86
  • 333
  • 562