【发布时间】:2012-01-06 14:32:05
【问题描述】:
根据site这个静态方法
static Point rectangular(float x, float y);
static Point polar(float radius, float angle);
调用私有构造函数Point(一种非静态方法),如下所示:
#include <cmath> // To get std::sin() and std::cos()
class Point {
public:
static Point rectangular(float x, float y); // Rectangular coord's
static Point polar(float radius, float angle); // Polar coordinates
// These static methods are the so-called "named constructors"
...
private:
Point(float x, float y); // Rectangular coordinates
float x_, y_;
};
inline Point::Point(float x, float y)
: x_(x), y_(y) { }
inline Point Point::rectangular(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)); }
};
编辑:我很难接受答案,因为我不知道哪个是正确的。
【问题讨论】:
-
构造函数更类似于静态成员函数而不是非静态成员函数。
-
一个静态成员函数可以调用一个非静态成员函数,如果它有一个对象可以调用它。
标签: c++ constructor static-methods