Solved

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

Posted on 2011-03-03
10
651 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 32

Expert Comment

by:phoffric
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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 32

Expert Comment

by:sarabande
Comment Utility
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
Comment Utility
>>That's a foolish statement

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

743 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

15 Experts available now in Live!

Get 1:1 Help Now