Initialization of C + + class special member variables (reference, static, constant member variable)

Source: Internet
Author: User

Some member variables have a special data type, and they are initialized differently from the member variables of the normal data type. These special types of member variables include:

A. References

B. Constants

C. Static

D. Static constants (integer type)

E. Static constants (non-integral type)

constants and references, which must be initialized through a parameter list.
The initialization of static member variables is also quite special, it is initialized outside the class and can no longer carry the static keyword, its essence is at the end of the text.

Refer to the following code and its comments:

    #include <iostream> using namespace std;                                                      Class Bclass {Public:bclass (): I (1), CI (2), RI (i) {}//For constant type member variables and reference member variables, must be initialized by a parameterized list  Ordinary member variables can also be placed in the function body, but the essence is not initialized, but a common operation operation--assignment, efficiency is low private:int                                  I                           Ordinary member variable const int CI;                                Constant member variable int &ri;                          Reference member variable static int si;                 static member variable//static int SI2 = 100;                   Error: static const int CSI can be initialized only if there is a statically constant member variable;            Static constant member variable static const int CSI2 = 100;                Initialization of static constant member variables (Integral type) (1) static const double CSD;      Static constant member variable (non-integral type)//static const double CSD2 = 99.9;            Error: Only static constant integer data members can be initialized in the class}; Note the following three lines: no longer with a static int bclass::si = 0;    Initialization of static member variables (Integral type)  const int BCLASS::CSI = 1; Initialization of a static constant member variable (Integral type) const double BCLASS::CSD = 99.9;      Static constant member variable initialization (non-integral type)//in initialization (1) in Csi2, according to famous master Stanley B.lippman, the following line is required.      But in VC2003 the following line will produce an error, and in VC2005, the following row is optional, which is related to the compiler.            const int BCLASS::CSI2;           int main () {Bclass B;      return 0;   }
Static members belong to the class scope, but not to the class object, and as with normal static variables, the program allocates memory and initializes it as soon as it runs, and the lifecycle and program are consistent.
Therefore, it is obviously unreasonable to initialize the static variable in the constructor of the class.
A static member is actually the same as a global variable, except that the compiler restricts its use to the class scope (not the class object, which is not part of the class object), and is initialized outside of the class's definition, not the scope of the class.


The following is the case where the member variable is a reference:
Because the reference is the meaning of the alias, the definition application must be externally defined and then initialized in the constructor initialization list.

    #include <iostream>    using namespace std;    Class A    {public    :        A (int i=3): m_i (i) {}        void print ()        {            cout<< "m_i=" <<m_i< <endl;        }    Private:        int m_i;    };    Class B    {public    :        B () {}        B (a& A): M_a (a) {}        void display ()        {            m_a.print ();        }    Private:        a& m_a;    };    int main (int argc,char** argv)    {        A A (5);        b b (a);        B.display ();        return 0;    }

So we can have a object in B,

[Note]: The initialization of a reference type member variable:

1 , cannot be initialized directly in the constructor, must use the initialization list, and the formal parameter must also be a reference type.

2 , a class that has a member variable of a reference type cannot have a default constructor. The reason is that a member variable of a reference type must be initialized when the class is constructed.
3 , if two classes want to share the data of the third class, consider taking the third class as a member variable of the reference type of these two classes.





Initialization of C + + class special member variables (reference, static, constant member variable)

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.