Solved

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

Posted on 2011-03-03
10
665 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Prototype for MessageDlg in Embacadero XE 2 2 35
TCP/IP Socket - connection close results in data lost 14 120
C Language combined operators 28 106
C++ mouse_event mouse look 7 70
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now