在返回拷贝的函数当中,类的各种函数的调用,尤其以拷贝形式返回时调用比较复杂,一下是结果

#include<iostream>
#include<iomanip>
#include<list>
#include<cmath>
#include<vector>
using namespace std;

struct Exmpl
{
    Exmpl()
    {
        std::cout<<"Exmpl()"<<endl;
    }
    Exmpl(const Exmpl &)
    {
        cout<<"Exmpl( const Exmpl&)"<<endl;
    }
    Exmpl& operator=(const Exmpl &rhe)
    {
        cout<<"operator=(const Exmpl&)"<<endl;
        return *this;
    }
    ~Exmpl()
    {
        cout<<"~Exmpl()"<<endl;
    }
};
Exmpl func3()
{
    Exmpl obj;  // 先构造一个对象,然后把这个对象复制到一个临时对象,接着析构这个对象,然后调用赋值操作符,然后析构临时对象...
    return obj;
}

void main()
{
    
    Exmpl eobj;
    eobj=func3();
    system("pause");
}

函数运行结果

Exmpl()

Exmpl()

Exmpl(const Exmpl&)

~Exmpl()

operator=(const Exmpl&)

~Exmpl()

~Exmpl()

原文地址:https://www.cnblogs.com/dyc0113/p/3202166.html