C + + static member variables

Source: Internet
Author: User

Today, I encountered a static member variable when I was making a piece of code. After a period of time, decisively turned out the "deep Exploration of C + + object Model", probably understand the problem lies. One of the revelations of this is that "on paper, you must be preach." First look at the problem! is actually a type of definition. The code is as follows:
Myapplication.cpp#include "Myapplication.h" #include <iostream>using std::cout;using Std::endl; Myapplication::myapplication (String _appname): AppName (_appname) {    if (self!=0)        return;    else        Self=this;} void MyApplication::p rintappname () {    Cout<<appname<<endl;} myapplication* myapplication::self=0;
Myapplication.h#ifndef myapplication_h_included#define myapplication_h_included#include <string>using std: : string; #define MYAPP myapplication::instance () class Myapplication{public:    myapplication (String _appname);    Static myapplication* instance () {return self;}    void Printappname ();p rivate:    static MyApplication *self;    string AppName;}; #endif//myapplication_h_included
See the myapplication* myapplication::self=0; I'm a little confused about this line. Why do we have to assign this value? Is it possible to assign a value to a class? I tried, and the answer was no. Check it out. C + + Specifies that static member variables of a class are assigned values outside the class. I have a question, why should it be stipulated?
Check out the in-depth exploration of C + + object-oriented model, and I find out why. The static member variables of the class first are shared by all class members, and there should be only one copy in memory. To implement this feature, C + + uses the static member variable as a global variable, but the scope of the global variable is within this class. So, instead of defining a static variable in a class definition, you are referring to a static variable. The initialization assignment outside the class actually contains the definition. I tried to get rid of this so-called out-of-class assignment, and the compiler would report "Undefined references to ..." error.
Second, I also made another confused, when the static variable is assigned to the value? I tested that the assignment of a static variable was done before the first object of that type was defined. What time is it? In fact, on the other, the assignment statement does not necessarily have to invoke execution at run time to complete. This "global variable" can be initialized when compiling, linking, or loading, and before the application runs, the "global variable" of the data segment has been assigned a good value. Of course, it can also be achieved through the. Init segment. After the specific situation, we will go to the bottom.

C + + static 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.