析构顺序

/*
 * main.cpp
 *
 *  Created on: 2016年8月28日
 *      Author: godsome
 */
#include <iostream>
using namespace std;

class B{
public:
    B(){
        cout << "default constructor" << endl;
    }
    ~B(){
        cout << "desturcted" << endl;
    }
    B(int i):data(i){
        cout << "constructed by parameter " << data << endl;
    }
private:
    int data;
};

B Play(B b){
    return b;
}

int main(){
    B t1 = Play(5);
    B t2 = Play(t1);
    return 0;
}

结果:

构造B(5)

析构B(5)

析构形参t1

析构t2

析构t1

原文地址:https://www.cnblogs.com/yingl/p/5817718.html