Learn from real coding (1)

Source: Internet
Author: User

Note: after graduating from college, I went to a software company to develop AutoCAD applications. In the days that followed, I wanted to use blog to record what I learned in my actual work (that is, the "experience" that people often say is very valuable !), I would like to share my experience with you and discuss relevant issues in depth. Another purpose of writing this document is to record my growth process.

Title: learn from real coding-1
Keywords: function overloading and default value in C ++
Abstract: function overloading and default values can be used to expand existing code and minimize code changes.

First, I need to write a function to add a circle in a drawing (the title of the document in AutoCAD:

ACAD: errorstatus drawcircle (const acgepoint3d & Center, double radius );

When I first started writing this function, considering that this function may only operate on the currently activated drawing, no parameter is provided.
Specifies the Drawing operation (AutoCAD is a multi-document structure, multiple drawing can be opened, but only one is activated),
Obtain the current drawing through another function within the function:

ACAD: errorstatus
Drawcircle (const acgepoint3d & Center, double radius )......{
// Get the Working Database
Acdbdatabase * pworkingdb = getworkingdatabase ();
// Append circle to this database
//...
}

 

At the beginning, this function worked very well and there was no problem. As the project progresses, I need to add a circle to any opened drawing (which may not be the currently activated drawing, in this case, you need to add the third parameter to specify the drawing to be added. I wrote the prototype of the drawcircle function in the second version:

ACAD: errorstatus drawcircle (const acdbdatabase * PDB, const acgepoint3d & Center, double radius );

The function overload feature in C ++ is used here. Similar functions can have the same function name, but different parameters (including the type and number of parameters) are required ). Because drawcircle () has already been used in many pieces of code, the existing code must fail to be compiled due to changes in the function prototype. No way. Find all the places in the code that use drawcircle () and replace them with the new version one by one. After several replacements, I found that in most cases, drawcircle () still operates on the currently activated drawing. In a few cases, other drawing operations are required. Isn't the default value of the parameters provided by C ++ exactly what I need? Use the currently activated drawing as the default value of the parameter. In this way, I wrote the prototype of the drawcircle function of the third version:

ACAD: errorstatus drawcircle (const acgepoint3d & Center, double radius,
Const acdbdatabase * PDB = getworkingdatabase ());

For the third version of drawcircle (), the existing code can be compiled without any changes, and the running results remain unchanged.

By comparing the first and third versions, the third version not only has all the functions of the first version, but also has the versatility not available in the first version, the advantage of the third version is obvious.

Postscript
As a result of my work, I am now getting started with some API maintenance and expansion content. When I see this article again, I suddenly found that adding parameters with default values to a function is an effective way to extend the current API. An important rule we need to follow when extending existing APIs is"Cannot (or as small as possible) destroy existing code", But adding a parameter with a default value for an API is very compliant with this rule.

History
05/05/2007 V1.1
1. Added the "Postscript part ". The content discussed in this article can be used as an effective method to extend the current API interface.

08/26/2006 V1.0
First version of the original 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.