#include <iostream> using namespace std; class Line { private : double length; public : double getLen(); void setLen(double length); Line(double length); ~Line() { cout << "Object is delete . " << endl; } }; int main() { Line line (10.00); cout << "Length of line : " << line.getLen() << endl; line.setLen(5.98); cout << "Cut to : " << line.getLen() << endl; Line line2(20.00); delete &line; delete &line2; system("pause"); return 0; } inline double Line::getLen() { return length; } inline void Line::setLen(double length) { this->length = length; } inline Line::Line(double length) { cout << "--Create an line--" << endl; this->length = length; }
运行时出错:
刚开始学C++ , 求指导答
解决方案
30
一个 malloc 对应一个 free
一个 new 对应一个 delete
一个 open/create 对应一个 close
你都没有 new, 怎么能去 delete 呢
一个 new 对应一个 delete
一个 open/create 对应一个 close
你都没有 new, 怎么能去 delete 呢
60
栈对象不需要delete