What are the benefits of a static member function for a class and when should it be used?

Source: Internet
Author: User
Tags mathematical functions sin
the static member function of the class brings what benefit and should be used at what time. Tags: math design pattern Algorithm API Programming store 2011-10-13 18:15 3382 person reading reviews (0) Favorite report Category: C/C + + programming (7)

When the behavior of a function is consistent for all derived classes, it can be declared as static.

The function of a static member function is basically equivalent to a global function with a namespace.

such as a mathematical class defines a number of functions can declare pseudo-static Math.sin (x) and so on

benefits are many, but all the benefits are based on: it is not related to the class instance, only with the class, do not need the this pointer.
for reentrant problems, there are non-static member functions as well. Why simply ask the reentrant question of a static function.
For example:
(1) can implement some special design patterns: such as singleton;

(2) because there is no this pointer, the callback functions of some system APIs can be encapsulated in the form of a static function to the inside of the class. Because the callback function of the system API is usually the kind of non-member function (the orphan function), there is no this pointer. For example, you can write a thread function inside the class for CreateThread to create threads, and if there is no static function, the callback function must be defined as a global function (non-static member function pointers cannot be converted to global function pointers), thus affecting the "encapsulation" of OO.

(3) can encapsulate some algorithms, such as mathematical functions, such as Ln,sin,tan, and so on, these functions do not necessarily belong to any object, so it feels better to call from a class, such as defining a mathematical function class math, calling Math::sin (3.14); If you want to use a non-static function, you must:
Math   math;
Math.sin (3.14);
Line is line, Just upset: Just for a non-state storage can be a mathematical function to introduce the construction of an object and the destruction of an object, of course, uncomfortable. And now that you have an object, you have to be careful. Defining copy constructors, copy assignment operators, and so on, is obviously inappropriate for something that is purely algorithmic.

(4) In short, from the perspective of Ooa/ood, all functions that do not need to be instantiated can have a deterministic behavior and should be designed to be static.
The above is just a few basic considerations, not absolute. There is only one point to the absolute: "A static function can be called without instantiation, and no non-static member can be invoked or manipulated." With this in mind, it's easier to make a decision when you need it in real-world programming and when it's appropriate.

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.