在Stroustrup的PPP书第12章中正确使用shared_ptr和make_shared

时间:2014-04-20 08:49:37

标签: c++ c++11 shared-ptr

我正在从Bjarne Stroustrup的编程原则和实践使用C ++的第12章开始练习。

本章中的图形界面库Simple_window.hGraph.h提供了一个围绕FLTK的简单包装。源代码位于http://www.stroustrup.com/Programming/Graphics/

要将Shape附加到Window,用户必须提供对Shape对象的引用,该对象必须在Window的生命周期内存在。相关成员函数具有以下签名:

void Graph_lib::Window::attach(Shape& s);

引用Difference in make_shared and normal shared_ptr in C++中的评论我想利用std::shared_ptrstd::make_shared来确保分配的Rectangle个对象(来自Shape )匹配Simple_window对象的生命周期(源自Window)。

第一个解决方案:

#include <iostream>
#include <vector>
#include <memory>
#include <stdexcept>
#include "Simple_window.h"
#include "Graph.h"

/*
Exercise 4 from Chapter 12
Programming Principles and Practice using C++, page 434

Draw a checkers board: 8-by-8 alternating white and red squares.
*/

int main()
{
    using namespace std;
    using namespace Graph_lib;
    using Graph_lib::Rectangle;

    try {
        Simple_window win(Point(20,20),400,300,"Exercise 12.4");
        vector<shared_ptr<Shape>> shapes;

        const auto width = 20;
        const auto height = 20;

        for (auto row = 0; row < 8; row++) {
            for (auto col = 0; col < 8; col++) {
                auto x = col * width;
                auto y = row * height;
                shared_ptr<Rectangle> r(new Rectangle(Point(x,y),width,height));
                shapes.push_back(r);
                if (row % 2 == 0) {
                    r->set_fill_color((col % 2 == 0) ? Color::white : Color::red);
                } else {
                    r->set_fill_color((col % 2 == 1) ? Color::white : Color::red);
                }
                win.attach(*r);
            }
        }
        win.wait_for_button();
    }
    catch(exception &e) {
        cerr << "Exception occured: " << e.what() << endl;
        return 1;
    }
    catch(...) {
        cerr << "Unknown exception occured" << endl;
        return 2;
    }
}

为了使智能指针的使用不那么明确,我使用用户定义的成员函数扩展Simple_window,该函数在将引用传递给Simple_window::attach(Shape&)之前将智能指针推送到向量中:

struct Shared_simple_window : Simple_window {
    Shared_simple_window(Point xy, int w, int h, const std::string& title )
        : Simple_window(xy, w, h, title) { }

    void attach(std::shared_ptr<Graph_lib::Shape>&& s)
        { shared_shapes.push_back(s); Simple_window::attach(*s.get()); }

private:
    std::vector<std::shared_ptr<Graph_lib::Shape>> shared_shapes;

};

try块中的代码现在变为

Shared_simple_window win(Point(20,20),400,300,"Exercise 12.4");

const auto width = 20;
const auto height = 20;

for (auto row = 0; row < 8; row++) {
    for (auto col = 0; col < 8; col++) {
        auto x = col * width;
        auto y = row * height;
        shared_ptr<Rectangle> r(new Rectangle(Point(x,y),width,height));
        if (row % 2 == 0) {
            r->set_fill_color((col % 2 == 0) ? Color::white : Color::red);
        } else {
            r->set_fill_color((col % 2 == 1) ? Color::white : Color::red);
        }
        win.attach(r);
    }
}
win.wait_for_button();

这样做有效,但我的问题是,如何在此示例中使用make_shared而不是new?我尝试用以下

替换shared_ptr声明
auto r = make_shared<Rectangle>(Rectangle(Point(x,y),width,height));

但是我收到错误use of deleted function Graph_lib::Rectangle::Rectangle(Graph_lib::Rectangle&&),可能是因为Rectangle不是为了移动而设计的。我应该延长Rectangle以使其有效吗?

1 个答案:

答案 0 :(得分:3)

您可以尝试将传递给构造函数的相同参数传递给make_shared,如下所示:

auto r = make_shared<Rectangle>(Point(x,y),width,height);