Solved

Why is this C++ class' int data member do not get initialized?

Posted on 2011-03-03
10
692 Views
Last Modified: 2012-05-11
I thought that the if default constructor does not exists, all POD's' get automatically initialized. But in this simple example the data value 'a' is not getting initialized.

#include "stdafx.h"
#include <iostream>
using namespace std;

class AClass
{
  int a;

 public:
   int getA() {return a;}
};

int _tmain(int argc, _TCHAR* argv[])
{
      int aValue;
      AClass ac;

      cout << ac.getA() << endl;
      cin >> aValue;

      return 0;
}
0
Comment
Question by:prain
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 35032505
That's a wrong assumption. You are responsible for initializing each and every member. What should the compiler initialize the values with anway? 0 or better 42?
0
 

Author Comment

by:prain
ID: 35032686
I do not expect a 42. That's a foolish statement. But at least I would like to see value 0 which can be considered as a default value.
0
 

Author Comment

by:prain
ID: 35032748
By the way, what is the default Constructor's job then in C++. I know what Java/C# default constructor doing. What's the use of the default constructor for C++ then if is is not initializing the class POD.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:phoffric
ID: 35032865
A default constructor is a constructor that can be called without supplying an argument.
A constructor creates an object  (i.e., defines a memory region for the object) and initializes (provided that you have initialization rules, of course).

If you want a default constructor that initializes the int a to 0, then add a default constructor. Below is one way to do this. It is a little more general than always forcing the 'a' to become 0, since it allows for an object to be constructed with a different initialization. But it is a default constructor since you can create your object without supplying any arguments to the constructor (in which case, 'a' is initialized to 0).
class AClass
{
  int a;

 public:
   AClass ( int x=0 ) : a(x) {}   // add constructor
   int getA() {return a;}
};

Open in new window

0
 

Author Comment

by:prain
ID: 35032892
I do not think a constructor would create memory regions. Memory region of the object should created prior to that because the constructor is also in the object's scope. So it cannot create a memory region for the object. But building the data components within the object is possible. JMO
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35032920
Do you prefer this wording?

"When an object of a class is created, C++ calls the constructor for that class. If no constructor is defined, C++ invokes a default constructor, which allocates memory for the object, but doesn't initialize it."
     http://www.fredosaurus.com/notes-cpp/oop-condestructors/constructors.html
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35032968
>> what is the default Constructor's job then in C++.
Here are other words that you may prefer:

"Default constructors are called implicitly to create class objects of static or automatic storage duration defined without an initializer, are called to create class objects of dynamic storage duration created by a new-expression in which the new-initializer is omitted, or are called when the explicit type conversion syntax is used."
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 125 total points
ID: 35034224
It seems that the crux of your question hasn't been addressed yet :

>> What's the use of the default constructor for C++ then if is is not initializing the class POD.

The thing to realize is that in C++, strict rules are followed for initialization (see paragraph 8.5 in the C++ standard).

One of those rules says that if no initializer is specified for a non-const non-static POD (plain old data) type (like int eg.), it will have an indeterminate value.

That is why if you do :

        int i;

i will have an indeterminate value.

Now, to link this to your question : the implicitly defined default constructor performs the same initializations as would be performed by a user-written default constructor with an empty initializer list.
Or in other words : an implicitly defined default constructor does not provide an initializer for a non-const non-static POD data member. So, such data members will have an indeterminate value after construction.


What's the use of the implicitly defined default constructor then ? It's really only useful in trivial cases, and most of the time you will want to provide your own constructors.
0
 
LVL 33

Expert Comment

by:sarabande
ID: 35034988
the implicitly defined default constructor is of good use if you don't use POD type members. in bigger projects with frameworks that is not unlikely and you can/should spare default constructor, copy constructor and assignment operator in those cases.

Sara
0
 
LVL 86

Expert Comment

by:jkr
ID: 35038299
>>That's a foolish statement

I love to meet intelligent and open minded people, too. Now, was I right or not?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
TCP/IP Socket - connection close results in data lost 14 151
Unable to start eclipse ? 17 161
c++, dynamic object by json 1 59
HTTPSendRequest with WinINet delays on first call 11 78
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question