C + + static class member

Source: Internet
Author: User
Tags class definition

I. What is a static class member

Access to a global object is sometimes necessary for all objects of a particular class type. The global object then destroys encapsulation: the object needs to support the implementation of a particular class abstraction. If the object is global, the normal user code can modify the value. Instead of defining a universally accessible global object, a class can define a class static member . Typically, non-static data members exist in each object of the class type. Unlike normal 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.

Class can also define static member functions. The static member function does not have the This parameter (the static member is part of the class but is not part of any object), it can directly access the static member of the owning class, but cannot use the non-static member directly.


Two. Advantages of using static members of a class

1) The name of the static member is in the scope of the class, so you can avoid conflicts with other classes ' members or global object names.

2) encapsulation can be implemented. A static member can be a private member, and a global object may not.

3) It is easy to see that a static member is associated with a particular class through a reading program. This visibility clearly shows the programmer's intentions.


Three. Defining static members

Add the keyword static to the member declaration before the member is set to static. Static members follow normal public/private access rules.

<span style= "FONT-SIZE:14PX;" >class a{public   :     static int getval () {return value;}     static void Setval (int);  Private:    static int value;} int a::value=0; Initialize </span>


Four. Using static members of a class

A static member can be called directly from a class by a scope operator, or indirectly through an object, reference, or pointer to an object of that class type.

A pg;

A *pg1=&pg;

int B;

B=pg. Getval ();

B=pg1->getval ();

B=a::getval ();


Five. member functions of the class

When the static member function is defined outside the class, there is no need to repeatedly specify the static keyword, which appears only at the declaration inside the class definition:

void A:: setval (int newval)

{

Value=newval;

}

Static members are not part of any object, so static member functions cannot be declared as const, after all, declaring a member function as const is a promise not to modify the object to which the function belongs. The static member function cannot also be declared as a virtual function.


Six. Static data members

Static data members can be declared as arbitrary types: constants, references, arrays, class types, and so on.

Static data members must be defined outside the class definition (exactly once), unlike normal data members, which are not initialized by a class constructor, but should be initialized at the time of definition.

This is defined in the following way:

Specify the type name first, followed by the fully qualified name of the member

int a::value=0; Initialization


1) Special integer const static member

Static members are generally not initialized in the definition body of a class like ordinary data members. One exception is that as long as the initialization is a constant expression, the integer const static data member can be initialized in the class's definition body:

<span style= "FONT-SIZE:14PX;" >class a{public   :     static int getval () {return value;}     static void Setval (int);  Private:    static const int num = ten;     int Sz[num];    static int value;} int a::value=0; Initialize </span>
<span style= "FONT-SIZE:14PX;" > const int A:: num;</span>

When a const static data member is initialized in the definition body of a class, the data member must still be defined outside the definition body of the class.


2) Static members are not part of a class object

Static members exist independent of any object and are not part of the class type object. The way they are used is not valid for non-static data members.

For example, the type of a static data member can be the class type to which the member belongs. Rather than a static member is qualified to declare a pointer or reference to its own class object.

Class C

{

Private

Static C tmp; Ok

C tmp; Wrong

};


In addition to the static data members There is a special mutable member, the use of the mutable member is also preceded by the type name plus mutable, mutable member can never be const, its value may be modified in the const member function.

C + + static class member

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