为什么这里没有调用复制构造函数?

时间:2021-05-30 13:54:46

标签: c++

对于下面的代码,Fun() 返回一个 Test 类型的临时对象

#include<iostream>
#include<typeinfo>

using namespace std;
    
class Test
{
private:
    int x;
    static int count;
public:
    Test(int i=0 )  {cout<<"sumit";}
    Test(const Test& rhs) : x(rhs.x) { ++count; cout<<"sum"; }
    static int getCount() { return count; }
};
    
int Test::count = 0;
    
Test fun()
{
    Test ob(7);

    return ob;
}
    
int main()
{   
    Test a=fun();
    cout<<typeid(fun()).name();
    cout<< Test::getCount();
    return 0;
}

未进行静态复制

代码的输出:

构造函数 4Test

0 个答案:

没有答案
相关问题