(go) c + + static, const, and static const and their initialization

Source: Internet
Author: User

The const -defined constants are freed after the function executes, and static constants defined by the statics are not freed when the function is executed. However, whether they are const or static, the content they define will be known to the system as the program ends.

Static is expressed statically. A static member function of a class, which is related to a class, is not related to a specific object of a class, and can call a static member function of a class, even if there is no specific object, member variable. A general static function is almost a global function, except that its scope is limited to the file containing it.

in C + + , static statically member variables cannot be initialized inside a class.

In C + + ,const constant member variables cannot be initialized at the class definition, only through constructor initialization lists, and must have constructors.

Const data members are constants only for the lifetime of an object , but are mutable for the entire class . Because a class can create multiple objects, different objects can have different values for their const data members.

You cannot initialize a const data member in a class declaration , because the compiler does not know what the value of the Const data member is when the object of the class is not created.

Const the initialization of a data member can only be done in the initialization table of the class's constructor . to build constants that are constant throughout the class, you should use the enumeration constants in the class, or the static const.

Such as:

classtest{ Public: Test (): A (0){}    enum{size1= -, Size2 = $ };Private:    Const intA//can only be initialized in the constructor initialization list, which seems to be seldom used???

Static intb//This variable is related to class Test and has nothing to do with instantiating objects like that.

Const Static intC//as with the static const int C, the same can be defined here (if you later need to use the variable in the class).

}

int Test::b = 0; // cannot be initialized as a member list and cannot be promoted at the definition because it does not belong to an object.

const int Test:: c = 0;// Note: When assigning a value to a static member variable, no static decoration is required. But the Const has to be added.

The const member function is primarily intended to prevent the content of the object from being modified. That is:int Fun () const , can not modify the object's data, can access the object's data, access member functions, can only be const , not non_const The member function.

The static member function, which is primarily intended as a global function for the scope of a class. You cannot access non-static data members of a class. The static member function of the class does not have this pointer, which results in:

1. Non -static member variables of the class cannot be accessed directly, and non-static member functions are called

2. cannot be declared as virtual

The following is an initialization question about the Static,const,static const, const static member.

1. Const member Initialization in class:

When you create a const in a class , you cannot give it an initial value. Like

Class Foo

{

Private

const int i = 100;    Error!!! The constructor for class Foo must be initialized.

Public

Foo () {}

......

};

This initialization is not compiled because the storage space is allocated in the class object, and the compiler cannot know what the const content is, so it cannot be used as a constant during compilation. This means that for a constant expression in a class, the const is as ifit had no effect in C. So this initialization work must take place in the constructor, and in a particular place in the constructor. Because the const must be initialized at the place where it was established, in the body of the constructor, theconst must have been initialized, otherwise, it would have to wait until it was initialized somewhere after the constructor body, which meant that the const was initialized in a few moments . Of course, there is no way to prevent a const value from being changed in different parts of the constructor body .

Constructor initialization expression

Class Foo

{

Private

const int i = 100;

Public

Foo () {...}

......

};

If the constructor is defined outside the class, you can write this:

Class Foo

{

Private

const int i;

Public

Foo ();

......

};

Foo::foo (): I (100) {...} Initialize list

2. initialization of static members in class:

A static variable in a class belongs to a class and does not belong to an object, it has only one copy during the entire program's run, so you cannot initialize the variable when you define the object, or you cannot initialize it with a constructor. The correct way to initialize it is:

< data type >< class name >::< static data member name > = < value >, for example

Class Foo

{

Private

static int i;

Public

Foo ();

......

};

int foo::i = +;

This shows that :

(1) initialization is performed outside the class body, and the front is not static, to avoid confusion with general static variables or objects.   

(2) Initialization without the access control of the member private, public,etc.   

(3) When initializing, the scope operator is used to indicate which class it belongs to, so a static data member is a member of the class, not a member of the object.

3. static const and const static members in the class are initialized:

are looking forStaticMember of the initialization learning material when I found out online there's a lot aboutStatic constThe initialized data for the member, which is the global static constant.The Const member is initialized in the constructor, and the static member needs to be initialized outside the class body, then the static const and const static where should the members be initialized? What's the difference between these two formulations? In fact , they are both the same. After borrowing theChinese version of thethinking in C + + 188 page and doing the relevant experiments, I think that if used within the class, thestatic const must be Initialization of the static const definition. Other things can be done inside and out of class.

Like what:

Class test{

Public

static const int MASK1;

const static int MASK2;

};

const int Test::MASK1 = 0xFFFF;

const int Test::MASK2 = 0xFFFF;

They are no different, though

One is a static constant ,

One is constant static,

The static is stored in the global variable area, in fact the final result is the same.   

may be handled differently within different compilers, but the final result is the same.

The following is a complete example:

1 #ifndef A_h_2 #defineA_h_3#include <iostream>4 using namespacestd;5 6  7 8 classa{9       Private:Ten           Static intAa//static data member declaration One           Static Const intCount//static constant data member declaration (which can be initialized here) A           Const intbb//Regular data members -        Public: -Ainta); the         Static voidPrint ();//Static member functions - }; -  -   +  -A::a (inta): BB (a) {//initialization of a regular member +AA + =1; A } at  -   -  -    voidA::p rint () { -cout <<"connt="<< count<<Endl; -cout <<"aa="<< AA <<Endl; in }; -  to   +  -    intA::AA =0;//Static member Definitions the Const intA::count = -;//Static constant member initialization * #endif $ Panax Notoginseng   -  the    intMain () { +A A (Ten); AA::p rint ();//accessing static member functions through classes theA.print ();//accessing static members through objects +}

Note: The new C + + standard is supported when the declaration of static const DataMember is placed in the header file , but VC6.0 does not support

(go) c + + static, const, and static const and their initialization

Related Article

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.