-2

What happens to uninitialized class members in c++? If I have to do this, what should I take care of?

#include <iostream> 
using namespace std;

class Foo {
    int attr1, attr2;   
public:
    Foo ();

Foo::Foo () {   attr1 = 5; }

Foo myThing(); 
Taylor
  • 1,797
  • 4
  • 26
  • 51

2 Answers2

4

What happens to uninitialized class members in c++?

They are default initialized, which in case of fundamental types like int means that it will have an indeterminate value.

what should I take care of?

You should take care to never read an indeterminate value.

eerorika
  • 232,697
  • 12
  • 197
  • 326
2

They are default-constructed.

For nested classes this means these have to have a constructor that can be called without arguments.

For plain old data members (int et. al.) this means they simply stay uninitialized as any other local variable would.

Kamajii
  • 1,826
  • 9
  • 21