Static member of the class

Source: Internet
Author: User

In practice, the following problems are encountered:

class CC {#pragma region Methodpublic:static void Method1();private:static void Method2();#pragma endregion Method#pragma region Propertystatic int p1= 3;static int p2;static short p3[...][...];#pragma endregion Property};

After compilation, the error1: p1 variable must be a constant before it can be initialized within the class. Variables are defined and declared. Pair

The static member declarations and definitions of the class must be separated, that is, they must be declared inside the class but not outside the class.

Is absolute, as the error message tells us that when a static member is a constant, the Declaration can be completed within the class.

And definition work.

...static const int p1 = 3;...

 

Likewise, p2 variables need to be defined outside the class. P3 static array members or pointers must also be defined outside the class.

Type Class: PropertyName here int CC: p2 = 3; short CC: p3 [] [];

 

Finally, remember that static members are shared within the scope of the class, and they are independent of the class instance.

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.