【发布时间】:2014-09-08 16:42:57
【问题描述】:
为什么这段代码会崩溃?
class Point {
public:
double x;
double y;
};
class Edge {
public:
Point org;
Point dst;
Edge(const Point& org, const Point& dest) {
this->org = org;
this->dst = dest;
}
};
class Box {
private:
std::vector<std::shared_ptr<Edge>> _edges;
void _init(const Point& lb, const Point& rt) {
std::cout << "Initialize Box ... " << std::endl;
// CRASH SOMEWHERE HERE ...
this->_edges.reserve(4);
this->_edges[0] = std::make_shared<Edge>(lb.x, lb.y, rt.x, lb.y);
this->_edges[1] = std::make_shared<Edge>(rt.x, lb.y, rt.x, rt.y);
this->_edges[2] = std::make_shared<Edge>(rt.x, rt.y, lb.x, rt.y);
this->_edges[3] = std::make_shared<Edge>(lb.x, rt.y, lb.x, lb.y);
std::cout << "Box object initialized" << std::endl;
}
public:
Box(const Point& lb, const Point& rt) {
this->_init(lb, rt);
}
};
【问题讨论】:
-
我建议在
this->_edges[0] = std::make_shared<Edge>(lb.x, lb.y, rt.x, lb.y);崩溃和错误段错误。
标签: c++ c++11 vector shared-ptr