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?

11
  • stackoverflow.com/questions/563221/… look at the first answer's section default constructors and the notes for PODs Commented Jan 23, 2013 at 22:23
  • 1
    Fundamental types don't have constructors. See stackoverflow.com/a/5113385/1801919. Commented Jan 23, 2013 at 22:23
  • The link that you provide is also for a Linux compiler, not VS2010 Commented Jan 23, 2013 at 22:23
  • 1
    @LucasMarcondesPavelski I think technically the variable is initialized, but not zero-initialized. Commented Jan 23, 2013 at 22:48
  • 1
    @juanchopanza : Indeed, though pedantically the correct terms here are that the variables are default-initialized but not value-initialized. Commented Jan 23, 2013 at 23:23

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Is that standardized or VC++-only?
This is known as value-initialization.
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.

2 Comments

so for example for Enums and all other class simple data types (and complex) I would have to do what juanchopanza suggests?
@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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.