我上课了
class ChartLine{
protected:
vector<Point> line; // points connecting the line
CString name; //line name for legend
CPen pen; //color, size and style properties of the line
};
Run Code Online (Sandbox Code Playgroud)
其中Point是一个结构
struct Point{
CString x;
double y;
};
Run Code Online (Sandbox Code Playgroud)
在main()我ChartLine用new运算符动态分配类型的对象.如果我delete之后使用,默认析构函数会~ChartLine()正确地处理(或清除)成员ChartLine::line(这是矢量顺便说一句),或者我必须~ChartLine()手动清除该向量?
提前致谢.干杯.
隐式创建的析构函数将调用所有成员的析构函数(按照它们在类中声明的相反顺序.)vector将自行清理.您不需要自己定义析构函数.
这就是为什么您应该更喜欢与RAII结合使用自动分配.当对象清理自己时,您的代码更安全,更容易.提示:不要使用new和delete,把它放在智能指针中!
std::auto_ptr<int> p(new int(5));
boost::shared_ptr<int> p = boost::make_shared<int>(5);
Run Code Online (Sandbox Code Playgroud)
这两个都会自动删除,现在你也是例外.(注意,上面两个不做同样的事情.还有更多类型的智能指针.)
| 归档时间: |
|
| 查看次数: |
545 次 |
| 最近记录: |