注册 登录
编程论坛 C++教室

关于复制构造函数的问题

mmppllhaha 发布于 2012-09-08 10:41, 271 次点击
下面这段程序的运行结果是
fun2()...
copy constructor
copy constructor
after fun2()...
为什么会有两个“copy constructor”呢?


程序如下:
#include<iostream>
using namespace std;

class Test
{
public:
    int a;
    Test(int x)
    {
        a=x;
    }
    Test(Test &test)
    {
        cout<<"copy constructor"<<endl;
        a=test.a;
    }
};
Test fun2()
{
    Test t(1);
    cout<<"fun2()..."<<endl;
    return t;
}
int main()
{
    Test t3=fun2();
    cout<<"before fun2()..."<<endl;
    return 0;
}
3 回复
#2
rjsp2012-09-08 12:20
换编译器
#3
mmppllhaha2012-09-08 13:06
回复 2楼 rjsp
感谢版主,换了一个编译器以后果然只有一个“copy constructor”了,为什么在VC6.0上会出现两个呢?它运行了什么呢?百思不得其解……
#4
pangding2012-09-08 22:57
vc6 bug 多多,换换更健康。
1