Default initialization for class data

I have the following code:

#include <iostream> using namespace std; class Base { private: int i; char ch; public: void showdata() { cout<<"Int:"<<i<<endl; cout<<"Char:"<<ch<<endl; } //int pub_data ; } ; int main() { Base ob; ob.showdata() ; //cout<<"Public Data:"<<ob.pub_data<<endl; return 0; } 

This program compiles and works fine. The output shows that I initialized to 0, and ch initialized to '\ 0'.
If you notice that I have commented on 2 statements in this program. First, the pub_data public data declaration and the second line inside the main print of this public data.
Now the problem is that if I uncomment these two lines, the data members of the class ie i, ch, pub_data do not seem to be initialized, and when printed, they display the values ​​of the unwanted messages. So my question is, what are the differences in public data making here?
I am using g ++ 3.4.6

+4
source share
4 answers

Neither int nor char is automatically initialized to 0. The fact that this happened is just luck.

You need to add a constructor that performs initialization:

 Base() : i(0), ch(0) {} 
+7
source

Absent. You just get lucky. Fundamental types remain uninitialized, so your i and ch , since the program is standing, may not always be 0.

It just happens, adding that the public member "messed it up." To fix your class, initialize the elements in the constructor initialization list:

 class Base { private: int i; char ch; public: Base(void) : i(0), ch(0) //, pub_data(0) {} void showdata() { cout<<"Int:"<<i<<endl; cout<<"Char:"<<ch<<endl; } //int pub_data ; } ; 

Now, when a Base is created i , ch and (when uncommenting) pub_data will be correctly initialized for significant values.

+2
source

Unlike Java or C #, where the memory allocated for newly created objects is ALWAYS set to zero, this does NOT happen in C ++. There are several rules that describe when the initialization of an object is guaranteed, and when it is not.

Consider the following example:

 class Base { private: int i; char ch; std::string str; public: Base() : i(0) //built-in fields remains unitialized. We should initialize it manually , ch('\0') //another built-in field //, str() //this call is redundant due automatic default constructors calls for all user-defined types {} void showdata() { cout<<"Int:"<<i<<endl; //valid only after manual initialization cout<<"Char:"<<ch<<endl; //valid only after manual initialization cout<<"String:"<<str<<endl; //always valid } //int pub_data ; } ; 

You must remember that ALL INPUT fields you must initialize manually in the class constructor.

PS The fact is that in the first case, your code works - this is a pure case.

0
source

The answer was already correct, but to make sure your values ​​are zero-initialized, you can also simply declare your object as

 Base ob(); // notice the parentheses here 

or

 Base ob{}; // this compiles only on c++11 

For more details check out this insightful answer: fooobar.com/questions/72 / ...

-one
source

Source: https://habr.com/ru/post/1299807/


All Articles