一次误用栈导致的错误。
来源:互联网 发布:2009网络流行歌曲大全 编辑:程序博客网 时间:2024/06/10 15:46
#include<iostream.h>// rough implementation class Point{public: Point(int _x=0,int _y=0) { x = _x; y = _y; } int x; int y;};class Rectangle:public Point{public: int width; int height; Point * leftUp; Point * rightDown; Rectangle(int width, int height, int x, int y); Rectangle(const Rectangle& other); Rectangle& operator=(const Rectangle& other); ~Rectangle();};inlineRectangle::Rectangle(int width,int height,int x,int y){ int _x = x; int _y = y; int _height = height; int _width = width; Point p1( _x,_y); leftUp = &p1; Point p2(_x+_width,_y+_width); rightDown = &p2;};inlineRectangle::~Rectangle(){// delete leftUp;// delete rightDown;}void main(){ Rectangle rect1(2,2,2,2); cout<<"("<<rect1.leftUp->x<<","<<rect1.leftUp->y<<")"<<endl; cout<<rect1.rightDown->x<<endl;};
问题解决:
栈空间在{}执行完后,会被释放。必须用new出来的,放在heap即堆里面才可以。
下面是源代码:
#include<iostream.h>class Point{public: Point(int _x=0,int _y=0) { x = _x; y = _y; } int x; int y;};class Rectangle:public Point{public: int width ; int height; Point * leftUp; Point * rightDown; Rectangle(int width, int height, int x, int y); Rectangle(const Rectangle& other); Rectangle& operator=(const Rectangle& other); ~Rectangle();};inline Rectangle::Rectangle(int width,int height,int x,int y){ int _x = x; int _y = y; int _height = height; int _width = width; leftUp = new Point(_x,_y); rightDown = new Point(_x+width,_y+height);};inlineRectangle::Rectangle(const Rectangle& other){ width = other.width; height = other.height; leftUp = new Point(other.leftUp->x,other.leftUp->y); rightDown = new Point(other.rightDown->x,other.rightDown->y);};inlineRectangle& Rectangle::operator=(const Rectangle& other){ if(this == &other) return *this; delete leftUp; leftUp = new Point(other.leftUp->x,other.leftUp->y); delete rightDown; rightDown = new Point(other.rightDown->x,other.rightDown->y); return *this;};inlineRectangle::~Rectangle(){ delete leftUp; delete rightDown;};void main(){ Rectangle rect1(2,2,2,2); cout<<"("<<rect1.leftUp->x<<","<<rect1.leftUp->y<<")"<<endl; cout<<"("<<rect1.rightDown->x<<","<<rect1.rightDown->y<<")"<<endl; Rectangle rect2(rect1); cout<<"("<<rect2.leftUp->x<<","<<rect2.leftUp->y<<")"<<endl; cout<<"("<<rect2.rightDown->x<<","<<rect2.rightDown->y<<")"<<endl; Rectangle rect3(3,3,3,3); rect3 = rect1; cout<<"("<<rect3.leftUp->x<<","<<rect3.leftUp->y<<")"<<endl; cout<<"("<<rect3.rightDown->x<<","<<rect3.rightDown->y<<")"<<endl;};
Github:
里面主要也没什么优化,主要就是把实验代码改成了h文件和cpp文件分开了。
https://github.com/juedaiyuer/GeekBand-CPP-1501-Homework/tree/master/G2015010148
0 0
- 一次误用栈导致的错误。
- gcc error: 'class A' has no member named 'b':误用多态导致的错误
- xcopy的一次误用及解决办法
- ntohl和htonl的一次误用
- ThreadLocal的误用可能会导致OutOfMemoryError
- android sendEmptyMessage()和sendMessage()误用导致handleMessage出现错误
- 一次由于设置错误,导致无法进入gnome的解决。
- 记一次因软件安装包问题导致的错误
- 一次windows漏洞导致的ora-07445错误整修记录
- 一次控制文件参数设置错误导致的restore controlfile 失败。
- 2016.7.7 一次低级错误导致的产品缺陷
- 误用const 所引起的错误
- int 和integer的误用导致 listview的不刷新
- 记一次排查mbstowcs误用引发的bug
- 误用Connection.setAutoCommit导致的数据库死锁问题
- ffmpeg 内av_frame_move_ref 误用导致的内存泄漏
- 一次笔误导致的bug
- Debug模式误用Opencv Release库的错误
- Linux中yum和apt-get用法及区别
- 通过eclipse查看、阅读hadoop2.4源码
- 关于hibernate 1+n问题的一些总结
- 搜索
- JAVA生成图片验证码
- 一次误用栈导致的错误。
- sas vname 返回最大值对应的变量名
- Spring注解@Component、@Repository、@Service、@Controller区别
- python2.7学习笔记(3) ——字符编码,列表,元组
- ISO14229 根据标识符读取数据ReadDataByIdentifier(22 Hex) service
- POJ 1061 青蛙的约会 (扩展欧几里得)
- vi 显示行号
- C++/STL中 vector中对 “=”赋值运算符的支持
- 手机充电器