Initialization methods for various types of member variables in C + + (mark-good) __c++

Source: Internet
Author: User

C + + class of different types of members based on whether static, const type of initialization method is not the same, write code often confused, online search data, all summed up. One, examples

-----------------Test.h----------------------------

#pragma once

class Test
{
private:
      int   var1;    int   var11= 4; Error initialization method
      const int   var2;    const int   var22 = 22222; Incorrect initialization methods, const members and reference members must be initialized in the initialization list (because they must be initialized at definition)
      static int   var3;
static    int   var3333=33333; error, only static constant member can assign value directly to initialize
      static const int   var4=4444;  correctly, static constant integer members (these three conditions are integral) can directly initialize the
      static const int   var44;
Public:
      Test (void);
       ~test (void);

----------Test.cpp----------------

#include ". \test.h"

int test::var3 = 3333333;//The correct initialization method for static members

//    int test::var1 = 11111;; error static member to initialize
//    int test::var2 = 22222; error
//    int test::var44 = 44444;// The wrong method, prompting for redefinition because this var44 is not the same as the Var44 modifier declared in the class
const int TEST::VAR44 = 44444;//correct

test::test (void): Var1 (11111), VAR2 (22222)   //Correct initialization method, VAR3 (33333) cannot initialize
{
    var1 = 11111;//correct, the normal variable can also be initialized here
    //var2 = 222222; Error because the const constant cannot be assigned a value and can only initialize the VAR3 =33 in the initialization list of the constructor
          
    ;   After testing, (1) If the VAR3 has been correctly defined, that is, the class defines the outer "int test::var3;" or "int test::var3=3333333;",
                //It is correct to assign a value here (note that it is assigned, not initialized);
                (2) If the VAR3 is defined correctly according to (1), the assignment here is incorrect because the VAR3 has not been defined.
}

Test::~test (void)




Two, simple summary
Some member variables have a special data type, and they are initialized in a way that is different from the member variables of the normal data type. These special types of member variables include:
A. Constant-type member variables: such as const int temp;
B. Reference-type member variables: int & temp;
C. Static member variables: such as static int temp;
D. Integral static constant member variables: such as static const int temp;
E. Non-integral static constant member variables: such as static const double temp;

Initialization of a constant member variable and a reference member variable must be done in the form of a constructor initialization list. It is not feasible to assign values to constant-type member variables and reference-type member variables in the constructor body.
The initialization of static member variables is also quite special.

Refer to the following code and the comments therein:
Initialization of Special Data member #include <iostream> using namespace std;                                  Class Bclass {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 by statically constant member variables;            Static constant member variable static const int CSI2 = 100;                Initialization of a static constant member variable (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 initialize Public:bclass () in the Class (): I (1), CI (2), RI (i)//for constant member variables and reference-type member variables, you must pass the { The way the parameterized list is initialized.
     

    It is not possible to assign values in the constructor body};

    Initialization of a static member variable (Integral type) int bclass::si = 0; Initialization of static constant member variables (inteGral type) const int BCLASS::CSI = 1;
   

    Initialization of a static constant member variable (non-integral type) const double BCLASS::CSD = 99.9;
    In the initialization (1) of the CSI2, according to Stanley B. Lippman the following line is necessary.

    But in the VC2003 if there is a line will produce an error, and in VC2005, the following line is optional, this is related to the compiler.  

    const int BCLASS::CSI2;

             int main (void) {Bclass b_class;
    return 0; }

Third, complete summary

1, ordinary variables: generally do not consider what the efficiency of the case can be assigned in the constructor. Consider the efficiency that can be done in the initialization list of the constructor.

Class CA
{public
:
 int data;
..... Public:
 CA ();
 ...
};
CA::CA ():d ata (0)//... #1 ... Initialize list mode
{
 //data = 0;//... #1 ..... Assign value way
};

2. Static statically variable:

The static variable belongs to the class and not to the object of the class, so there is only one variable regardless of how many objects the class is instantiated.

Class CA
{public
:
 static int sum;
..... Public:
 CA ();
 ...
};
int ca::sum=0;//......# ... Define and initialize outside of a class


3. Const constant Quantity:
The const constant needs to be initialized when it is declared. Therefore, you need to initialize the variable when it is created. Typically used in constructors ' initialization lists.

Class CA
{public
:
 const int max;
..... Public:
 CA ();
 ...
};
CA::CA (): Max (+)
{
 ...
}


4, Reference reference variables:
Reference and const variables are similar. Needs to be initialized at the time of creation. is also done in the initialization list. But you need to be careful with the reference type.

Class CA
{public
:
 int& counter;
..... Public:
 CA (int i);
 ...
};
CA::CA (int i): Counter (i)
{
 ...
}


5. Const static INTEGRAL variable:
C + + is privileged for both const and static and for shaping variables. can be initialized directly in the definition of a class. Short can, but float can not oh.

Class CA
{public
:
 //static const FLOAT fmin = 0.0; 
 Only static const integral the data members can be initialized within a class
 const static int nmin = 0;
..... Public:
 ...
};


To sum up, there are four places that can be initialized:
1, in the definition of a class, with only const and static and integral variables.
2, in the constructor initialization list of the class, including the Const object and the Reference object.
3, initialized outside the definition of a class, including static variables. Because it is the only variable that belongs to the class.
4, ordinary variables can be assigned in the interior of the constructor by way of assignment. Of course it's not efficient.
Data Source: http://www.douban.com/note/66957147/http://blog.csdn.net/jenghau/article/details/4752735, modified

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.