通读The C++ Programming Language, 4th Edition,有一个类Circle
继承自Shape
这样
class Circle : public Shape {
public:
Circle(Point p, int rr); // constructor
Point center() const { return x; }
void move(Point to) { x=to; }
void draw() const;
void rotate(int) {} // nice simple algorithm
private:
Point x; // center
int r; // radius
};
和Shape
班级
class Shape {
public:
virtual Point center() const =0; // pure virtual
virtual void move(Point to) =0;
virtual void draw() const = 0; // draw on current "Canvas"
virtual void rotate(int angle) = 0;
virtual ˜Shape() {} // destructor
// ...
};
令我困惑的部分是Circle
的构造函数:
Circle(Point p, int rr);
返回类型在哪里?在之前的所有构造函数中,都指定了返回类型void
。我找不到 C++ 隐式返回类型(如 C 的隐式 int 规则)
身体/初始化在哪里?所有以前的构造函数要么通过成员列表初始化 ( :
) 或在函数体中初始化{}
。我在这里都没有看到,所以我想知道这些值是如何初始化的。
构造函数不指定返回类型,也不返回任何内容。
它没有主体,因为它只是声明 - 定义将在其他地方,看起来像
Circle::Circle (Point p, int rr)
{
...
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句