C ++ class design considerations (III): class methods and implementation functions

Source: Internet
Author: User

An implementation function is a function that independently completes a specific task, such as dot product, differential product, and calculation of the matrix's determinant. To implement a function, follow these principles:

1. Single Responsibility Principle: A function can have only one function and cannot contain multiple functions. However, you can call other functions to complete specific tasks. A common error example is the statistical Implementation of container objects. Its Code includes both the traversal of container sub-objects and the statistical processing of a single sub-object.

2. Standard Data Type principle: the parameter type of the implemented function must be the standard data type built in the language and cannot be customized by the user.

3. Principle of zero external dependency: the implementation function must not depend on any external variables, global variables, class member variables, or other global functions. It can only depend on the passed function parameters; if the function implementation code needs to handle errors, you can specify the error processing function pointer in the parameter list.

4. Contractual implementation principle: the entry to function implementation must provide pre-condition verification, including function parameter check. The exit of function implementation must provide post-Condition Verification. If the function has output parameters, Initialize all output parameters as the default values after the previous condition verification is passed (in this way, the result can be excluded from randomness );

 

A scheduling function is a function that implements a function or other scheduling functions to complete a specific task. The main purpose of a scheduling function is to provide the operation scenario required to implement the function. Scheduling functions must follow the following principles:

1. Zero implementation principle: a scheduling function cannot contain any implementation. It can only use implementation functions or other scheduling functions to complete specific tasks;

2. hierarchical scheduling principle: Scheduling functions can only use other scheduling functions with a lower level than themselves, rather than cross-level scheduling. that is, the King schedules the city lord and the City Master dispatches the knight, but the King cannot schedule the knight.

 

When designing C ++ classes, the first principle to be followed is the separation of class methods and their implementations, that is, the unavailability of scheduling, implementation is not scheduled (the class method has been bound to a specific class and depends on the specific object state, and its code reuse degree must be smaller than the corresponding implementation function ). The class method is a scheduling function and must also comply with the scheduling function principle. The class method completes a specific function by taking the object state as the scenario and calling the corresponding implementation function.

 

Instance code:

 

Template <typename T>

Inline const t vector2dotimpl (const t x0, const t y0, const t X1, const t Y1)

{

Return x0 * X1 + y0 * Y1;

}

 

Template <typename T>

Class cvector2

{

...

T dot (const cvector2 <t> & RHs) const

{

Return vector2dotimpl (m_x, m_y, RHS. m_x, RHS. m_y );

}

...

T m_x;

T m_y;

};

 

Hu leqiu

2010/8/9

Http://blog.csdn.net/hlqyq

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.