初始化基类的引用成员

时间:2015-10-04 15:14:01

标签: c++ oop abstract-class

经过长时间的禁欲,我目前正在尝试刷新我的C ++技能。因此,我正在实施一个小型的绘图圆圈图像程序。

场景: 我有一个名为 Shape 的基类,它基本上由一个成员组成,该成员是对表示中心的 Point 的引用。 形状,除中心外,还有半径。

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 构造函数(在此示例中为字母&#34; a&#34;),则对中心 Point 的引用似乎设置(输出B)。此外, Circle 似乎都引用相同的?!有人可以解释为什么会发生这种情况,以及处理这种情况的适当方法是什么?

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)

2 个答案:

答案 0 :(得分:1)

这里有一个问题:

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存储了对p Circle参数的引用。在构造结束时,参数消失。

我可能不存储引用,而是存储值。这样你就有了参数的副本。复制Point并不是非常昂贵。

答案 1 :(得分:0)

Shape类的正确接口可能如下所示:

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;

};

同样,你的Circle类也应该采用const Point&amp;在构造函数中。

关于你的主程序,可能会做这样的事情

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)。希望有所帮助。