将指向具有初始化列表的对象的指针传递到另一个对象ctor

时间:2017-08-17 10:31:53

标签: c++

我正在编写对象适配器设计模式的示例。这是我的适应班

class Adaptee
{
public:
    Adaptee(Coord x1, Coord y1, Coord x2, Coord y2)
    {
        x1_ = x1;
        y1_ = y1;
        x2_ = x2;
        y2_ = y2;
        std::cout << "LegacyRectangle:  create.  (" << x1_ << "," << y1_ << ") => ("
            << x2_ << "," << y2_ << ")" << std::endl;
    }


    void oldDraw()
    {
        std::cout << "LegacyRectangle:  oldDraw.  (" << x1_ << "," << y1_ <<
            ") => (" << x2_ << "," << y2_ << ")" << std::endl;
    }
private:
    Coord x1_;
    Coord y1_;
    Coord x2_;
    Coord y2_;
};

在我的适配器类中,我将一个Adaptee指针传递给构造函数,并在私有数据区域中声明一个Adaptee指针字段。问题是我对如何在Adapters ctor中包含Adaptee的初始化列表感到困惑。这是我的尝试

class Adapter : public Target
{
public:
    Adapter(Adaptee* adaptee) : _adaptee(adaptee)
    {
        std::cout << "RectangleAdapter: create.  (" << _adaptee->x1_ << "," << y <<
            "), width = " << w << ", height = " << h << std::endl;
    }

    virtual void draw()
    {
        std::cout << "RectangleAdapter: draw." << std::endl;
        oldDraw();
    }

private:
    Adaptee* _adaptee;
};

0 个答案:

没有答案