18.10管理未命名对象
2019-03-25 本文已影响0人
Jianbaozi
#include "Graph.h"
#include "Simple_window.h"
template<class T>class Vector_ref {
vector<T*>v;
vector<T*>owned;
public:
Vector_ref() {}
Vector_ref(T*a, T*b , T*c, T*d = 0);
~Vector_ref(){
for (int i = 0; i < owned.size(); ++i)
delete owned[i];
}
void push_back(T&s) { v.push_back(s); }
void push_back(T*p) { v.pop_back(p); owned.push_back(p); }
T&operator[](int i) { return *v[i] };
const T&operator[](int i)const { return *v[i]; }
int size()const { return v.size() }
};
int main()
{
using namespace Graph_lib;
Simple_window win{ Point{100,100},800,600,"91Wanbabe" };
Graph_lib::Vector_ref<Graph_lib::Rectangle>vr;
for (int i = 0; i < 16; ++i) {
for (int j = 0; j < 16; ++j) {
vr.push_back(new Graph_lib::Rectangle{ Point{i * 30,j * 30},30,30 });
vr[vr.size() - 1].set_fill_color(Color{ i * 16 + j });
win.attach(vr[vr.size() - 1]);
}
}
win.wait_for_button();
}
QQ截图20190325170629.jpg