C + +: The member variable declaration order of the class is the same as that of the initialization order

Source: Internet
Author: User

The default initialization order for class members is in the order of declaration, and if you initialize a member variable with an initialization list, you must follow the declaration order of the member variable;

Otherwise, when alternating between variables, an uninitialized variable is created to assign the other variable;

GCC also emits a warning, such as: ' Class::m_xxx ' will be initialized after [-wreorder]

Code:

* * * * * BInsertSort.cpp * *  Created on:2014 April 15 
 *      author:spike/
      
#include < iostream>  
#include <string>  
      
using namespace std;  
      
Class Base {  
private:  
    int m_i;  
    int m_j;  
Public:  
    Base (int i): M_j (i), m_i (M_j) {}  
    base (): M_j (0), m_i (M_j) {}  
    int get_i () {return m_i;}  
    int Get_j () {return m_j;}  
};  
      
int main () {  
    base obj ();  
    Std::cout << obj.get_i () << Std::endl  
            << obj.get_j () << Std::endl;  
    return 0;  
}

Output:

4202398  
98

Author: csdn Blog spike_king

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.