(Learning notes) Static constant member variable Initialization
Source: Internet
Author: User
**************************************** ***************************
It is now possible to initialize integral constant static members inside the class structure.
This is useful when the constants is used in the class structure after the initialization.
(This is effective when this constant is used after initialization)
For example ==>
Class myclass {
Static const int num = 100;
Int elements [num];
...
};
Note that you still have to define space for a constant static member that is initialized within a class definition:
Const int myclass: num; // No initialization here
*/
# Pragma warning (Disable: 4530)
# Include <iostream>
Using namespace STD;
Class
{
Public:
A (){
For (INT I = 0; I <size; I ++)
Num [I] = I;
}
Void print (){
For (INT I = 0; I <size; I ++)
Cout <num [I] <"" <Endl;
}
PRIVATE:
Static const size = 10;
// Note: Because size is a const constant, it must be assigned a value at the first definition.
Int num [size];
};
Const int A: size;
// Note: Because size is a static member of a class, it should be defined outside the class body (to obtain a utility similar to that of global variables)
Int main ()
{
A;
A. Print ();
Return 0;
}
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.