//This is called : Named Constructor Idiom.
//Note that this way, at least as implemented below, is Just as fast as directly
//call a constructor.
class Point {
public:
static Point _Rectanglular(float x, float y); //recrangular coord's.
static Point _Polar(float radius, float andgle);
//If you wanna derived class access this constructor, set //'protected'.
private:
Point(float x, float y);
float m_x, m_y;
};
inline Point::Point(float x, float y)
:m_x(x), m_y(y){ }
//Just rename the constructor.
//Returns a temp object.
inline Point Point::_Rectanglular(float x, float y)
{
return Point(x, y);
}
inline Point Point::_Polar(float radius, float angle)
{
return Point(radius * std::cos(angle), radius * std::sin(angle));
}
Named constructor
最新推荐文章于 2019-07-22 08:36:19 发布