这难道是编译器的bug?
时间:2011-12-27
来源:互联网
C/C++ code
#include <iostream> using namespace std; class Point { public: Point(int=0,int=0); Point(const Point&); void displayxy(); ~Point(); private: int X,Y; }; Point::Point(int x,int y) { X=x; Y=y; cout<<"Constructor is called!"; displayxy(); } Point::Point(const Point& p) { X=p.X; Y=p.Y; cout<<"Copy constructor is called!"; displayxy(); } Point::~Point() { cout<<"Destructor is called!"; displayxy(); } void Point::displayxy() { cout<<"("<<X<<","<<Y<<")"<<endl; } Point func(Point p) { int x=10*2; int y=10*2; Point pp(x,y); return pp; } int main() { Point p1(3,4); Point p2=p1; p2=func(p1); return 0; }
在codeblocks的运行结果:
Constructor is called!(3,4)
Copy constructor is called!(3,4)
Copy constructor is called!(3,4)
Constructor is called!(20,20)
Destructor is called!(20,20)
Destructor is called!(3,4)
Destructor is called!(20,20)
Destructor is called!(3,4)
Process returned 0 (0x0) execution time : 0.061 s
Press any key to continue.
在vc的运行结果:
Constructor is called!(3,4)
Copy constructor is called!(3,4)
Copy constructor is called!(3,4)
Constructor is called!(20,20)
Copy constructor is called!(20,20)
Destructor is called!(20,20)
Destructor is called!(3,4)
Destructor is called!(20,20)
Destructor is called!(20,20)
Destructor is called!(3,4)
Press any key to continue
为什么会这样呢?
作者: zoopang 发布时间: 2011-12-27
这块是明显需要调用拷贝构造函数的
作者: chenph_210 发布时间: 2011-12-27
作者: mscf 发布时间: 2011-12-27
作者: mscf 发布时间: 2011-12-27
BUG倒谈不上,一般情况下因为这样的优化产生的问题较少。
作者: ww884203 发布时间: 2011-12-27
作者: supermegaboy 发布时间: 2011-12-27
作者: Demon__Hunter 发布时间: 2011-12-27
我算服了csdn了,从7楼一直吃到14楼~~~
这个是csdn的bug?还是人为的?
作者: Demon__Hunter 发布时间: 2011-12-27
作者: cattycat 发布时间: 2011-12-27
不同编译器在临时对象返回上实现可能不一样,标准在这方面没有规定吧。
++
code::blocks用的是gcc编译器,与VC对程序的实现是不同的
作者: keiy 发布时间: 2011-12-27
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28