Static member declarations, defining

Source: Internet
Author: User
Class A
{
Public
static const int CI;
};
const int A::CI = 10;

int main ()
{
cout << A::ci;
return 0;
}
Compile with no problem, run will error. , because CI is undefined, memory is not allocated, and object CI is not established as an entity.
"Note" for unused objects (including functions), you can declare and not define, you need to define when you need to use, allocate memory, become an entity

Class A
{
Public
static const int CI;
};
const int A::CI = 10;

int main ()
{
cout << A::ci;
return 0;
}
So everything OK

VC6 does not support specifying an initial value in the body of a class definition (not a definition, although formally similar, specifying that the initial value is not allocated memory, the entire class definition is not allocated memory, when the class object is created, the memory is allocated when the class object is defined),
Standard notation for C + +

Class A
{
Public
static const int CI = 10;
};
const int A::CI,/*= 10*/;

int main ()
{
cout << A::ci;
return 0;
}

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.