Static data members in C + + classes, static member functions

Source: Internet
Author: User

In the C + + class that talks about static, we can define static members, static member functions in the Class! C++primer: Static members It is not like ordinary data members, static data members exist independent of any object of the class, and each static data member is an object associated with the class and is not associated with an object of that class! This sentence may be quite awkward, in fact, you can understand: each static data member can be regarded as an object of the class, and not with the object defined by the class has any relationship! Let's take a look at the static data members in the Class!

When it comes to data members, the first thing we want to think about is how to define a static data member, which is stored in the program's Quiescent store, not on the stack space. Since it is a static data member, the keyword static is essential, for example:

Static. h file # include<iostream>#include<string>using namespacestd;classperson{Private:    stringname; Static intAge ; Public: Person (Const string&nm): Name (nm) {}voidPrint () {cout<<name<<" is"<<age<<Endl; }};intPerson::age= -;Static. cpp file # include"stdafx.h"#include"static.h"#include<iostream>using namespacestd;int_tmain (intARGC, _tchar*argv[]) { Person person ("Tom"); Person.    Print (); cout<<Endl; return 0;}

A static data member age is defined in the person class, noting that static data members cannot be initialized in a class and must be defined outside of the class to initialize! Note that static data members are not initialized by the class constructor! As shown in the code above: int person::age=20 is defined outside the class; no more static in front of it. If there are multiple static data members in a class, the order in which the static data member is initialized is initialized in the order in which the static data members are declared in the class, and after initialization, the static data member can be used. We can call the static data member directly from the class through the scope operator, or indirectly through the object, reference, or pointer to the object of that class type (in which case the static data member must be public access, if the definition is not possible under private access).

When it comes to static data members, one thing you have to mention is the special const static member. As described above, the static members of a class, like normal data members, cannot be initialized in the body of the class's definition. Can only be initialized outside of the class. The static member of the const int can be initialized inside the class definition body. Remember that it must only be a const int type, and a const string, double. Look at the following code:

Static. h header file # include<iostream>#include<string>using namespacestd;classperson{Private:    stringname; Static Const intAge= -; Static stringaddress; Public: Person (Const string&nm): Name (nm) {}Static stringAddress () {returnaddress; }    voidPrint () {cout<<name<<" is"<<Age ; }};stringperson::address="Beijing";Static. cpp file # include"stdafx.h"#include"static.h"#include<iostream>using namespacestd;int_tmain (intARGC, _tchar*argv[]) { Person person ("Tom"); Person.    Print (); cout<<"and live in"<<Person .    Address (); cout<<Endl; return 0;}

Only age can initialize in the class definition body, and address is not possible. This code cannot be run on VC6.0, it is not supported. Can be run on vs2008. There is a note in C++primer that the const static data member must be defined outside the definition body of the class after it has been initialized in the definition body of the class. In fact, this is not to be. The above code does not have this code implementation, in fact, plus go is also possible, no relationship. Also, the type of a static data member can have a class type that the member belongs to, and a non-static data member is qualified as a pointer or reference to its own class object. For example, the class defines a bit as follows:

classperson{Private:    stringname; Static Const intAge= -; Static stringaddress; StaticPerson Person1; person*Person2; Person Person3; Public: Person (Const string&nm): Name (nm) {}Static stringAddress () {returnaddress; }    voidPrint () {cout<<name<<" is"<<Age ; }};

Static data members in C + + classes, static member functions

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.