Differences between user functions, non-static member functions of classes, and static member functions

Source: Internet
Author: User

The declared functions in the class have three meanings compared to the class:
1. This pointer is available
2. The function is in the Action Area of the class.
3. Private part of the category

4. can be inherited

Non-static member functions have 1234
Static member functions have 234
Youyuan function has 3

The difference between a static member function and a friend function is only the difference in the role region.
When a friend function is used globally, the same name conflict may occur.
Although the namespace can solve some problems, I prefer to use static member functions =

1. The address of the static member function can be stored by the common function pointer, while the common member function address needs to be stored by the class member function pointer. Example:
Class base {
Static int func1 ();
Int func2 ();
};

INT (* pf1) () = & base: func1; // common function pointer
INT (base: * pf2) () = & base: func2; // member function pointer

2. static member functions cannot call non-static members of a class. Because the static member function does not include the this pointer.

3. static member functions cannot be declared as virtual, const, and volatile functions at the same time. Example:
Class base {
Virtual static void func1 (); // Error
Static void func2 () const; // Error
Static void func3 () volatile; // Error
};

The last point is that static members can be accessed independently, that is, they can be accessed without creating any object instance.

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.