Class static, const, static const, const static member Initialization

Source: Internet
Author: User

I have learned C ++ for a long time, but I have never been able to write c ++ code, so many things have been forgotten. During my vacation, I found C ++ learning materials to review and write down some study notes for my future review. The following are my learning notes on class static, const, static const, and const static member initialization.
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;
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 ){......}
 
2. initialize static members in the class:
The static variable in the class belongs to the class, rather than to an object. It has only one copy during the running process of the program, so it does not

Variables can be initialized when objects are defined, that is, they cannot be initialized using constructors. The correct initialization method is:
<Data type> <class name >:< static data member name >=< value>, for example
Class foo
{
PRIVATE:
Const 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? This aroused the author's interest. After reading the relevant materials and conducting relevant experiments on the Internet, the author confirms that the initialization method of members modified with the static keyword is the same as that of members modified with the static keyword, they must be initialized in vitro, regardless of whether static is before or after the const. For example:
Class test {
Public:
Static const int mask1;
Static const int mask2;
};
Const int test: mask1 = 0 xFFFF;
Const int test: mask2 = 0 xFFFF;
I still don't know the differences between the two methods. The following is a description about their differences on the Internet, which is for your reference only:
No difference.
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.

 

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.