Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-03-03
10
Medium Priority
?
710 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 500 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 35

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.
Suggested Courses

783 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