Regular member functions of C + + classes and static member functions __jquery

Source: Internet
Author: User

1 Regular member functions

1.1 Declare:< type identifier > function name (parameter list) const;

1.2 Description:

1 Const is a part of the function type, in the implementation part also should take this keyword;

2 The Const keyword can be used to distinguish the overloaded functions;

3 A constant member function cannot update a member variable of a class, nor can it invoke a member function in a class that does not have a const modifier, and can only invoke a regular member function, but may be called by other member functions;

4) in particular : a regular object can only access the const member functions in a class (except for the implicit constructors and destructors that are automatically invoked by the system)

1.3 Routines:

Class a{
private:
    int W, h;
Public:
    int getValue () const;
    int GetValue ();
    A (int x, int y): w (x), h (y) {}
    A () {}
    ~a () {}
};
int A::getvalue () const{return
    w*h;
}
int A::getvalue () {return
    w+h;
}
int main ()
{
    const A A (1, 2);
    A c (1,2);
    cout << a.getvalue () << Endl;	Invoke the const member function
    cout << c.getvalue () << Endl;	Call non-const member function return
    0;
}

2 Static member functions

A member function that is decorated with static can only be defined once, and is shared by all objects of the same class, which is an act of classes that is independent of the object and has the following characteristics:

1 static function members can not directly access the class Non-static data members and non-static member functions, only through the object name (passed by the parameter) to access;

2 when the static member function is implemented outside the class, there is no need to add static modification, or error;

3 Outside the class, you can invoke the static member function of the class by using the object name as well as the class name.

Class b{
private:
    int x;
    int y;
    static int count;
Public:
    B (): X (0), y (0) {
        count++;
    }
    B (int xx, int yy): X (xx), Y (yy) {
        count++;
    }
    static int Getobjcount ();
int b::count = 0;
int B::getobjcount () {return
    count;
}
int main ()
{
    cout << b::getobjcount () << Endl;
    B B1;
    B B2 (a);
    cout << b1.getobjcount () << Endl;
    cout << b2.getobjcount () << Endl;
    cout << b::getobjcount () << Endl;
    return 0;
}



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.