Static member function (object-oriented static keyword)

Source: Internet
Author: User

Static member functions

As with static data members, we can also create a static member function that serves the entire service of a class rather than the specific object of a class. Static member functions, like static data members, are internal implementations of the class and are part of the class definition. Ordinary member functions generally imply a this pointer, which points to the object of the class itself, because ordinary member functions are always specific to the specific object of a class. Typically, this is the default. such as the function fn () is actually THIS->FN (). However, compared to a normal function, a static member function does not have the this pointer because it is not associated with any object. In this sense, it cannot access non-static data members that belong to a class object, nor can it access a non-static member function, which only calls the rest of the static member functions. Here is an example of a static member function.

1#include <iostream>2 using namespacestd;3  4 classMyClass5 {6  Private:7       intA, B, C;8       Static intSum//declaring a static data member9  Public:TenMyClass (intAintBintc); One       Static voidGetsum ();//declaring a static member function A }; -  - intMyclass::sum =0;//defining and initializing static data members theMyclass::myclass (intAintBintc) - { -       This->a =A; -       This->b =b; +       This->c =C; -sum + = A+b+c;//non-static member functions can access static data members + } A  at voidMyclass::getsum ()//implementation of static member functions - { -       //cout<<a<<endl;//error code, a non-static data member -cout<<"sum ="<<sum<<Endl; - } -  in intMain () - { toMyClass M (1,2,3); + m.getsum (); -MyClass N (4,5,6); the n.getsum (); * myclass::getsum (); $ Panax Notoginseng       return 0; -}

For static member functions, you can summarize the following points:

• A function definition that appears outside the class body cannot specify a keyword static;

• Static members can access each other, including static member functions to access static data members and access static member functions;

• Static member functions and static data members can be accessed arbitrarily by non-static member functions;

• Static member functions cannot access non-static member functions and non-static data members;

• Because there is no additional overhead for this pointer, the static member function has a slight increase in speed compared to the global function of the class;

• Call a static member function to call a static member function with the member access operator (.) and (-) for a class object or a pointer to a class object, or you can use the following format directly:

< class name >::< static member function name > (< parameter table >)

Invokes the static member function of the class.

Static member function (object-oriented static keyword)

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.