初始化基class的引用成员
Initialize reference member of base class
经过长时间的禁欲,我目前正在尝试刷新我的 C++ 技能。因此,我正在实施一个微型绘图圆圈图像程序。
场景:
我有一个名为 Shape 的基础 class,它基本上由一个成员组成,它是对 Point 的引用,表示中央。 Circle 是一个 Shape,它除了圆心之外还有一个半径。
class Shape
{
public:
Shape(Point& p);
~Shape();
Point& get_center() const;
private:
Point& center;
};
-
class Point
{
public:
Point();
Point(float x, float y);
Point(short d);
~Point();
float get_x() const;
float get_y() const;
friend std::ostream& operator<< (std::ostream& stream, const Point& p);
private:
short dimension;
float x, y;
};
//.cpp
std::ostream& operator<<(std::ostream &strm, const Point &p) {
return strm << "Point(" << p.get_x() << "," << p.get_y() << ")";
}
-
class Circle :
public Shape
{
public:
Circle(Point p);
Circle(Point p, float radius);
~Circle();
float get_radius() const;
private:
float radius;
typedef Shape super;
};
成员的初始化执行如下:
Circle::Circle(Point p, float rad) : Shape(p), radius(rad)
{
std::cout << "Called circle constructor with center point " << p << std::endl;
}
Shape::Shape(Point& p) : center(p) {
std::cout << "Called Shape constructor with point " << p << "a" <<std::endl;
}
如果我是 运行 下面的代码,似乎中心变量设置不正确,导致下面给出的输出 A。奇怪的是,如果我将更多输出附加到 Shape 构造函数(在本例中为字母 "a"),则对中心 Point[=42= 的引用] 似乎已设置(输出 B)。此外,两个 Circles 似乎引用相同的 Point?!有人可以解释为什么会这样吗?处理这种情况的适当方法是什么?
Circle& cir1 = *(new Circle(Point(400, 550), 50));
Circle& cir2 = *(new Circle(Point(300, 550), 80));
vector<Circle> circles { cir1, cir2 };
cout << "Iterating circles" << endl;
for (auto& cir : circles)
{
cout << "Radius: "<< cir.get_radius() << " center: " << cir.get_center() << endl;
}
输出A:
Called Shape constructor with point Point(400,550)
Called circle constructor with center point Point(400,550)
Called Shape constructor with point Point(300,550)
Called circle constructor with center point Point(300,550)
Iterating circles
Radius: 50 center: Point(1.87436e-38,1.87419e-38)
Radius: 80 center: Point(1.87436e-38,1.87419e-38)
输出 B:
Called Shape constructor with point Point(400,550)a
Called circle constructor with center point Point(400,550)
Called Shape constructor with point Point(300,550)a
Called circle constructor with center point Point(300,550)
Iterating circles
Radius: 50 center: Point(300,550)
Radius: 80 center: Point(300,550)
这里有问题:
Circle::Circle(Point p, float rad) : Shape(p), radius(rad)
{
std::cout << "Called circle constructor with center point " << p << std::endl;
}
Shape::Shape(Point& p) : center(p) {
std::cout << "Called Shape constructor with point " << p << "a" <<std::endl;
}
因为 Shape
存储了对 Circle
的 p
参数的引用。构建结束,参数消失
我可能不会存储一个引用,而是一个值。这样你就有了参数的副本。复制一个Point
不是很贵。
Shape class 的正确界面可能如下所示:
class Shape
{
public:
explicit Shape(const Point& p); //const Point& ensures you can all that constructor with an R-value
virtual ~Shape();
const Point& get_center() const;
private:
Point center;
};
同样,你的Circleclass也应该在构造函数中使用const Point&。
关于你的主程序,也许做这样的事情
std::shared_ptr<Circle> cir1(new Circle(Point(400, 550), 50));
std::shared_ptr<Circle> cir2( new Circle(Point(300, 550), 80));
vector< std::shared_ptr<Circle> > circles { cir1, cir2 };
cout << "Iterating circles" << endl;
for (auto cir : circles)
{
cout << "Radius: "<< cir->get_radius() << " center: " << cir->get_center() << endl;
}
关于您的代码的问题是您引用了 R 值(例如,参见 http://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c )。希望对您有所帮助。
经过长时间的禁欲,我目前正在尝试刷新我的 C++ 技能。因此,我正在实施一个微型绘图圆圈图像程序。
场景: 我有一个名为 Shape 的基础 class,它基本上由一个成员组成,它是对 Point 的引用,表示中央。 Circle 是一个 Shape,它除了圆心之外还有一个半径。
class Shape
{
public:
Shape(Point& p);
~Shape();
Point& get_center() const;
private:
Point& center;
};
-
class Point
{
public:
Point();
Point(float x, float y);
Point(short d);
~Point();
float get_x() const;
float get_y() const;
friend std::ostream& operator<< (std::ostream& stream, const Point& p);
private:
short dimension;
float x, y;
};
//.cpp
std::ostream& operator<<(std::ostream &strm, const Point &p) {
return strm << "Point(" << p.get_x() << "," << p.get_y() << ")";
}
-
class Circle :
public Shape
{
public:
Circle(Point p);
Circle(Point p, float radius);
~Circle();
float get_radius() const;
private:
float radius;
typedef Shape super;
};
成员的初始化执行如下:
Circle::Circle(Point p, float rad) : Shape(p), radius(rad)
{
std::cout << "Called circle constructor with center point " << p << std::endl;
}
Shape::Shape(Point& p) : center(p) {
std::cout << "Called Shape constructor with point " << p << "a" <<std::endl;
}
如果我是 运行 下面的代码,似乎中心变量设置不正确,导致下面给出的输出 A。奇怪的是,如果我将更多输出附加到 Shape 构造函数(在本例中为字母 "a"),则对中心 Point[=42= 的引用] 似乎已设置(输出 B)。此外,两个 Circles 似乎引用相同的 Point?!有人可以解释为什么会这样吗?处理这种情况的适当方法是什么?
Circle& cir1 = *(new Circle(Point(400, 550), 50));
Circle& cir2 = *(new Circle(Point(300, 550), 80));
vector<Circle> circles { cir1, cir2 };
cout << "Iterating circles" << endl;
for (auto& cir : circles)
{
cout << "Radius: "<< cir.get_radius() << " center: " << cir.get_center() << endl;
}
输出A:
Called Shape constructor with point Point(400,550)
Called circle constructor with center point Point(400,550)
Called Shape constructor with point Point(300,550)
Called circle constructor with center point Point(300,550)
Iterating circles
Radius: 50 center: Point(1.87436e-38,1.87419e-38)
Radius: 80 center: Point(1.87436e-38,1.87419e-38)
输出 B:
Called Shape constructor with point Point(400,550)a
Called circle constructor with center point Point(400,550)
Called Shape constructor with point Point(300,550)a
Called circle constructor with center point Point(300,550)
Iterating circles
Radius: 50 center: Point(300,550)
Radius: 80 center: Point(300,550)
这里有问题:
Circle::Circle(Point p, float rad) : Shape(p), radius(rad)
{
std::cout << "Called circle constructor with center point " << p << std::endl;
}
Shape::Shape(Point& p) : center(p) {
std::cout << "Called Shape constructor with point " << p << "a" <<std::endl;
}
因为 Shape
存储了对 Circle
的 p
参数的引用。构建结束,参数消失
我可能不会存储一个引用,而是一个值。这样你就有了参数的副本。复制一个Point
不是很贵。
Shape class 的正确界面可能如下所示:
class Shape
{
public:
explicit Shape(const Point& p); //const Point& ensures you can all that constructor with an R-value
virtual ~Shape();
const Point& get_center() const;
private:
Point center;
};
同样,你的Circleclass也应该在构造函数中使用const Point&。
关于你的主程序,也许做这样的事情
std::shared_ptr<Circle> cir1(new Circle(Point(400, 550), 50));
std::shared_ptr<Circle> cir2( new Circle(Point(300, 550), 80));
vector< std::shared_ptr<Circle> > circles { cir1, cir2 };
cout << "Iterating circles" << endl;
for (auto cir : circles)
{
cout << "Radius: "<< cir->get_radius() << " center: " << cir->get_center() << endl;
}
关于您的代码的问题是您引用了 R 值(例如,参见 http://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c )。希望对您有所帮助。