Solved

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

Posted on 2011-03-03
10
697 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 34

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

751 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