如果函数的传入参数和返回值是对象,那么这个过程中会发生哪些我们未曾注意过的细节呢?
我在VS2008下做了如下实验,并做出了简单的分析,有不足或者不准确的地方,欢迎大家拍砖,我会及时修正相关内容。
一、函数的传入参数是对象。
#include <tchar.h>
#include <iostream>
using namespace std;
class Person
{
public:
Person(int nAge):m_nAge(nAge){ cout << _T("创造了一个年龄为") << m_nAge << _T("的人") << endl; }
Person(Person& Somebody):m_nAge(Somebody.m_nAge + 10) { cout << _T("复制创造了一个年龄为") << m_nAge << _T("的人") << endl; }
~Person() { cout << _T("析构了一个年龄为") << m_nAge << _T("的人") << endl; }
private:
int m_nAge;
};
void Test(Person Somebody)
{
return;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
Person Mike(20);
Test(Mike);
return 1;
}
上述代码的执行结果如下:
对于上述代码做如下简单解释:
Person Mike(20);
创造了一个年龄为20的人
Test(Mike);
将Mike作为Test函数的实参传入,Person Somebody的复制构造函数被调用。
当Test函数返回后,Somebody这个对象的生命周期结束,它的析构函数被调用。
当_tmain函数返回后,Mike这个兑现的声明周期结束,它的析构函数被调用。
上述代码中存在一个比较关键的地方,那就是在调用Test函数的时候,涉及到将Mike对象拷贝给Somebody对象。
#include <tchar.h>
#include <iostream>
using namespace std;
class Person
{
public:
Person(int nAge):m_nAge(nAge){ cout << _T("创造了一个年龄为") << m_nAge << _T("的人") << endl; }
Person(Person& Somebody):m_nAge(Somebody.m_nAge + 10) { cout << _T("复制创造了一个年龄为") << m_nAge << _T("的人") << endl; }
~Person() { cout << _T("析构了一个年龄为") << m_nAge << _T("的人") << endl; }
public:
int m_nAge;
};
void Test(Person Somebody)
{
return;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
Person Mike(20);
Test(Person(50));
return 1;
}
上述代码的执行结果如下:
我们可以从结果中看到,这次并没有拷贝构造函数被调用,个人猜测,Person(50)这个临时构造的对象就是Somebody这个对象,它们实际上是一个对象,存在于Test函数的栈中,至于这是C++本身的标准还是VS2008帮助我们优化之后的结果,尚未进行研究。
二、函数的返回值是对象
#include <tchar.h>
#include <iostream>
using namespace std;
class Person
{
public:
Person(int nAge):m_nAge(nAge){ cout << _T("创造了一个年龄为") << m_nAge << _T("的人") << endl; }
Person(Person& Somebody):m_nAge(Somebody.m_nAge + 10) { cout << _T("复制创造了一个年龄为") << m_nAge << _T("的人") << endl; }
~Person() { cout << _T("析构了一个年龄为") << m_nAge << _T("的人") << endl; }
public:
int m_nAge;
};
Person Test()
{
Person Jack(30);
return Jack;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
Person Tom = Test();
return 1;
}
上述代码的执行结果如下:
上述代码的简单解释:
Test函数被执行,Person Jack(30)被执行,构造函数被调用。
Person Tom = Test();Test函数执行完成之后,将Jack对象拷贝给Tom对象,所以,Tom的拷贝构造函数被执行,返回值赋值完成之后,Jack对象的生命周期结束,析构函数被执行,_tmain函数返回之后,Tom对象的生命周期结束,析构函数被执行。
#include <tchar.h>
#include <iostream>
using namespace std;
class Person
{
public:
Person(int nAge):m_nAge(nAge){ cout << _T("创造了一个年龄为") << m_nAge << _T("的人") << endl; }
Person(Person& Somebody):m_nAge(Somebody.m_nAge + 10) { cout << _T("复制创造了一个年龄为") << m_nAge << _T("的人") << endl; }
~Person() { cout << _T("析构了一个年龄为") << m_nAge << _T("的人") << endl; }
public:
int m_nAge;
};
Person Test()
{
Person Jack(30);
return Jack;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
Test();
cout << _T("-----------------------") << endl;
return 1;
}
上述代码的执行结果如下:
简单的分析上述代码:
我们看到,代码中并没有定义一个对象来接受Test函数的返回值,但是系统仍然为我们创造了一个临时的对象来接受Test函数的返回值,但是因为这个临时的对象并没有被我们用到,所以,它马上就被析构了。
#include <tchar.h>
#include <iostream>
using namespace std;
class Person
{
public:
Person(int nAge):m_nAge(nAge){ cout << _T("创造了一个年龄为") << m_nAge << _T("的人") << endl; }
Person(Person& Somebody):m_nAge(Somebody.m_nAge + 10) { cout << _T("复制创造了一个年龄为") << m_nAge << _T("的人") << endl; }
~Person() { cout << _T("析构了一个年龄为") << m_nAge << _T("的人") << endl; }
public:
int m_nAge;
};
Person Test()
{
return Person (30);
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
Person Tom = Test();
cout << _T("-----------------------") << endl;
return 1;
}
上述代码的执行结果如下:
我们可以从结果中看到,这次并没有拷贝构造函数被调用,个人猜测,Person(30)这个临时构造的对象就是Tom这个对象,它们实际上是一个对象,存在于_tmain函数的栈中,至于这是C++本身的标准还是VS2008帮助我们优化之后的结果,尚未进行研究。
三、函数的传入参数和返回值都是对象。
#include <tchar.h>
#include <iostream>
using namespace std;
class Person
{
public:
Person(int nAge):m_nAge(nAge){ cout << _T("创造了一个年龄为") << m_nAge << _T("的人") << endl; }
Person(Person& Somebody):m_nAge(Somebody.m_nAge + 10) { cout << _T("复制创造了一个年龄为") << m_nAge << _T("的人") << endl; }
~Person() { cout << _T("析构了一个年龄为") << m_nAge << _T("的人") << endl; }
public:
int m_nAge;
};
Person Test(Person Somebody)
{
return Somebody;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
Person Mike(20);
Person Tom = Test(Mike);
return 1;
}
上述代码的执行结果如下:
上述代码的简单解释如下:
Person Mike(20);
构造函数被执行
将Mike作为实参传入到Test函数当中,实参到形参的拷贝过程调用了Somebody的拷贝构造函数。
Test函数执行结束,Somebody对象被拷贝给Tom对象,调用了Tom对象的拷贝构造函数。
Somebody的生命周期结束,析构函数执行。
Tom对象的生命周期结束,析构函数被执行。
Mike对象的生命周期结束,析构函数被执行。