Use of const static and static const

Source: Internet
Author: User
  View articles    
C ++ static, const, and static const and their initialization
C ++ static, const, and static const and their initialization

(17:27:47)

Reprinted
Tags:

Cstaticconst initialize it
Classification: learning C ++

The space of a constant defined by const is released after the function is executed, while the static constant defined by static is not released after the function is executed.

Static indicates static. Static member functions of a class. member variables are related to classes rather than specific objects of the class. even if no specific object exists, static member functions and member variables of the class can be called. A general static function is almost a global function, but its scope is limited to the files containing it.

In C ++, static member variables cannot be initialized within the class.

In C ++, const constant member variables cannot be initialized in class definitions. They can only be initialized through the const initialization list and must have constructors.

Const data members are constants only within the lifetime of an object, but they are variable for the entire class. Because the class can create multiple objects, the values of the const data members of different objects can be different. Therefore, the const data member cannot be initialized in the class declaration, because the compiler does not know the value of the const data member when the class object is not created.

Const data member initialization can only be performed in the class constructor initialization table. To create a constant in the entire class, use the enumerated constant in the class or static Const.

For example:

Class Test

{

Public:

Test (): A (0 ){}

Enum {size1 = 100, size2 = 200 };

PRIVATE:

Const int A; // It can only be initialized in the constructor initialization list. This seems to be seldom used ???

Static int B;

Const static int C; // The same as static const int C; can be defined here (if this variable is needed in the class ).

}

Int test: B = 0; // It cannot be initialized by the member list, and cannot be promoted in the definition, because it does not belong to an object.

Const int test: C = 0; // Note: when assigning values to static member variables, do not add static modification. But const needs to be added.

The const member function is mainly used to prevent object content modification. That is: int fun () const; the data of the object cannot be modified, but the data of the object can be accessed. when accessing a member function, it can only be a const, not a member function of non_const.

Static member functions are mainly used as global functions of class scopes. It cannot be a non-static data member of the category. The static member function of the class does not have the this pointer, which causes:

1. You cannot directly access non-static member variables of a class and call non-static member functions.

2. cannot be declared as virtual

 

 

The following describes the initialization of static, const, static const, and const static members.

 

1. Const member initialization in the class:

When a const is created in a class, the initial value cannot be given. Image

Class foo

{

PRIVATE:

Const int I = 100; // Error !!!

Public:

Foo (){}

......

};

This initialization method cannot be compiled. Because the storage space is allocated in the class object, 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, const does not work as it does in C. Therefore, this initialization must occur in the constructor, and must be somewhere special in the constructor. Because the const must be initialized at the place where it is created, the const must have been initialized in the constructor body; otherwise, it will only wait, it is not initialized until somewhere after the const body. Of course, it cannot be prevented from changing the value of const in different places of the constructor.

Constructor initialization expression

Class foo

{

PRIVATE:

Const int I = 100;

Public:

Foo (){......}

......

};

If the constructor is defined outside the class, you can write it as follows:

Class foo

{

PRIVATE:

Const int I;

Public:

Foo ();

......

};

Foo: Foo (): I (100) {...} // initialization list

2. initialize static members in the class:

Static variables in a class belong to a class and do not belong to an object. They only have one copy during the running of the program. Therefore, they cannot be initialized when an object is defined, it cannot be initialized using constructors. The correct initialization method is:

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

Class foo

{

PRIVATE:

Static int I;

Public:

Foo ();

......

};

Int FOO: I = 100;

This indicates:

(1) initialization is performed in the external class without static before, so as to avoid confusion with general static variables or objects.

(2) during initialization, the access control letter private and public of the member are not added.

(3) during initialization, the scope operator is used to indicate the class to which it belongs. Therefore, static data members are members of the class, not members of the object.

3. initialize static const and const static members in the class:

When looking for static member initialization learning materials, I found a lot of information about static const members on the Internet, that is, the initialization of Global static constants. The const member must be initialized in the constructor, while the static member must be initialized in the class body. Where should the static const and const static members be initialized? What are the differences between the two statements? They are both the same. After reading thinking in C ++ on the 188 page of the Chinese version and conducting relevant experiments, I think static const must be initialized in the definition of static const if used in the class. In other cases, it works inside and outside the class.

For example:

Class test {

Public:

Static const int mask1;

Const static int mask2;

};

Const int test: mask1 = 0 xFFFF;

Const int test: mask2 = 0 xFFFF;

They have no difference, although

One is a static constant,

One is constant static,

Static data will be stored in the global variable area. In fact, the final result is the same.

It may be processed differently in different compilers, but the final result is the same.

 

The following is a complete example:
# Ifndef a_h _
# Define a_h _
# Include <iostream>
Using namespace STD;

 

Class {
PRIVATE:
Static int AA; // static data member declaration
Static const int count; // The Declaration of static data members (which can be initialized here)
Const int BB; // regular data member
Public:
A (int );
Static void print (); // static member function
};

 

A: A (int A): BB (a) {// initialization of regular members
AA + = 1;
}

 

Void A: Print (){
Cout <"connt =" <count <Endl;
Cout <"AA =" <AA <Endl;
};

 

Int A: AA = 0; // static member Definition
Const int A: Count = 25; // initialize a static member
# Endif

 

Int main (){
A A (10 );
A: Print (); // access static member functions through classes
A. Print (); // access static members through objects
}

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.