Author: Zhu Jincan
Recently, some new ideas have been made on the design of function return values. A while ago, I checked my code first. My code is roughly as follows (by memory ):
# Include <stdio. h>
# Include <float. h>
# Include <string>
Using std: string;
# Include <vector>
Using std: vector;
/*! @ Struct stExtent
* @ Brief
*
* Geographic range struct to perform operations on geographic ranges, such as merging
* @ Author zjc
* @ Version 0.1
* @ Date 2010.08.10
*/
Struct stExtent
{
StExtent ()
{
M_fMinX =-FLT_MAX;
M_fMaxX = FLT_MAX;
M_fMinY =-FLT_MAX;
M_fMaxY = FLT_MAX;
}
/*!
* @ Brief merge two geographic ranges
*
* @ Param [in] Another geographic range of Other
* @ Return new geographic range
*/
StExtent operator + (stExtent & Other)
{
M_fMinX = std: min (m_fMinX, Other. m_fMinX );
M_fMaxX = std: max (m_fMaxX, Other. m_fMaxX );
M_fMinY = std: min (m_fMinY, Other. m_fMinY );
M_fMaxY = std: max (m_fMaxY, Other. m_fMaxY );
Return * this;
}
Void operator = (stExtent & Other)
{
M_fMinX = Other. m_fMinX;
M_fMaxX = m_fMaxX, Other. m_fMaxX;
M_fMinY = m_fMinY, Other. m_fMinY;
M_fMaxY = m_fMaxY, Other. m_fMaxY;
}
/**
* @ Brief X minimum value
*/
Float m_fMinX;
/**
* @ Brief X maximum value
*/
Float m_fMaxX;
/**
* @ Minimum value in the brief Y Direction
*/
Float m_fMinY;
/**
* @ Brief Y: Maximum Value in the direction of Y
*/
Float m_fMaxY;
};
/*! @ Class CDrawObj
* @ Brief
*
* Display the object base class
* @ Author zjc
* @ Version 0.1
* @ Date 2010.08.10
*/
Class CDrawObj
{
Public:
CDrawObj ()
{
M_Extent.m_fMinX = 0.0f;
M_Extent.m_fMinY = 0.0f;
M_Extent.m_fMaxX = 100366f;
M_Extent.m_fMaxY = 100366f;
}
/*!
* @ Brief get the geographic range of the displayed object
* @ Return: displays the geographical range of an object.
*/
StExtent & GetExtent ()
{
Return m_Extent;
}
Protected:
Private:
/**
* @ Brief: Display attribute array
*/
Std: vector <stProperty> m_vecPropertys;
/**
* @ Brief geographic range
*/
StExtent m_Extent;
};
/*! @ Class CDrawObj
* @ Brief
*
* Display the object base class
* @ Author zjc
* @ Version 0.1
* @ Date 2010.08.10
*/
Class CDrawObj
{
Public:
CDrawObj ()
{
M_Extent.m_fMinX = 0.0f;
M_Extent.m_fMinY = 0.0f;
M_Extent.m_fMaxX = 100366f;
M_Extent.m_fMaxY = 100366f;
}
/*!
* @ Brief get the geographic range of the displayed object
* @ Param Extent: geographical range obtained
* @ Return: displays the geographical range of an object.
*/
Void GetExtent (stExtent & Extent)
{
Extent = m_Extent;
}
Protected:
Private:
/**
* @ Brief: Display attribute array
*/
Std: vector <stProperty> m_vecPropertys;
/**
* @ Brief geographic range
*/
StExtent m_Extent;
};
We recommend that you change the GetExtent function of the CDrawObj class to the following:
StExtent & GetExtent ()
{
Return m_Extent;
}
I said, "Is this to Facilitate the Implementation of chained expressions? "I mean: According to the header modification method, the external can call: stExtent TmpExtent = Obj1. GetExtent () + Obj1. GetExtent (); the header says:" Not all is, according to your practice, you must first define a stExtent variable and then pass it into the function. "I said:" This is because of my habits. I think the value returned by the urn is used to determine whether the operation is successful ". However, this header does make sense. Because the return value of this operation is void, it is more reasonable to directly return the operation result value.
Today, I saw such a class function written like this:
Struct stProperty
{
StProperty ()
{
M_strName = _ T ("");
}
/**
* @ Brief display attribute name
*/
Std: string m_strName;
};
/*!
* @ Brief: A property name is provided to search for this property.
*
* @ Param [in] the attribute name specified by strProName
* @ Return attribute
*/
StProperty Invalid_Property;
StProperty SearchProperty (const std: string strProName) const
{
// M_vecPropertys is a class attribute array that first loops through the attribute array to find the qualified array,
// If the error does not exist, an exception is thrown and the error attribute is returned.
Try
{
For (size_t I = 0; I <m_vecPropertys.size (); I ++)
{
If (strProName = m_vecPropertys [I]. m_strName)
{
Return m_vecPropertys [I];
}
}
}
Catch (...)
{
}
Throw _ T ("cannot find Property ");
Return Invalid_Property;
}
I think this design is not reasonable. If I design it, I will design this function as follows:
BOOL SearchProperty (const std: string strProName, stProperty & prop ),
Use the return value of the BOOL variable to determine whether such an attribute exists. Use the variable prop to save the search result.
For this reason, I have summarized some experiences in designing functions: first, determine whether the operation is successful or whether the value exists. If not, you can directly return the operation result (that is, the result is returned by the urn statement instead of the output parameter). After all, it is convenient for external calls. If necessary, the operation result value should be returned through the output parameter.