Static-const class member variables

Source: Internet
Author: User

[Link to this article]

Http://www.cnblogs.com/hellogiser/p/static-const.html

[Analysis]

Const data members must be initialized in the constructor initialization list;

Static data members must be initialized globally and cannot have static delimiters, such as int class: size = 100;

For static const and const static members, only the int type can be initialized internally. Any type can be initialized outside the class, but cannot be initialized in the constructor list, you need to add the const keyword. For example, const int class: size = 100;

[Code]

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  /*
Version: 1.0
Author: hellogiser
Blog: http://www.cnblogs.com/hellogiser
Date: 2014/9/20
*/

# Include "stdafx. H"
# Include "iostream"
Using namespace STD;


Class myclass
{
Public:
Myclass (INT size): size1 (size)
{

}
~ Myclass ()
{

}
Const int size1; // member initializer list
Static int size2; // outside class

Static const int size3 = 150; // only int can be initialized inside class
Static const int size4;
};

Int myclass: size2 = 100; // static
Const int myclass: size4 = 200; // static const


Void test_class_const_static_variable ()
{
Myclass OBJ (50 );
Cout <obj. size1 <Endl;
Cout <obj. size2 <Endl;
Cout <obj. size3 <Endl;
Cout <obj. size4 <Endl;
}

Int main ()
{
Test_class_const_static_variable ();
Return 0;
}
/*
50
100
150
200
*/

[Reference]

Http://blog.csdn.net/gukesdo/article/details/7435805

Static-const class member variables

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.