The static object in the C ++ class does not seem to be a class object.

Source: Internet
Author: User
# Include <stdio. h> Class  Cdemocls {  Public  :  Int  Idata;  Public  : Cdemocls () {idata = 0  ; Printf (  "  % S \ n  "  ,__ Function __);} ~ Cdemocls () {printf ( "  % S \ n  "  ,__ Function __);}};  Class  Cdemocls2 {  Public  :  Static   Class  Cdemocls CLS;  Static   Int  A;  Public  : Cdemocls2 () {printf ( "  % S \ n  "  ,__ Function __);} ~ Cdemocls2 () {printf (  "  % S \ n  "  ,__ Function __);}};  Int  Main () {cdemocls2 OBJ;  //  OBJ. Cls. idata = 5;  //  The following error is reported:  /* Testclsstatic. OBJ: Error lnk2019: the external symbol that cannot be parsed "public: static class cdemocls cdemocls2: CLS "(? CLS @ cdemocls2 @ 2vcdemocls @ A), which is referenced in function _ main using testclsstatic.exe: Fatal error lnk1120: 1 external command that cannot be parsed  */      Return   0  ;} 

Define two classes. The second class wants to use a static Class 1 object. Now the compilation can run and display:

Cdemocls2: cdemocls2cdemocls2 ::~ Cdemocls2

It seems that there is no cdemocls. When the main function is used with variables of the static class, an error is reported directly, and external symbols that cannot be parsed.

Really depressing. I just want the second class not to repeat the first class. Taking the first class as a global variable, I always think it is difficult to manage more global variables.

Alas, I did not expect such a simple requirement.

In addition, static in the class cannot be directly initialized, which is the most annoying. Only initialize once.

Related Article

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: 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.