如果函数的传入参数和返回值是对象
2013-03-22 22:16
666 查看
如果函数的传入参数和返回值是对象,那么这个过程中会发生哪些我们未曾注意过的细节呢?
我在VS2008下做了如下实验,并做出了简单的分析,有不足或者不准确的地方,欢迎大家拍砖,我会及时修正相关内容。
一、函数的传入参数是对象。
上述代码的执行结果如下:
对于上述代码做如下简单解释:
Person Mike(20);
创造了一个年龄为20的人
Test(Mike);
将Mike作为Test函数的实参传入,Person Somebody的复制构造函数被调用。
当Test函数返回后,Somebody这个对象的生命周期结束,它的析构函数被调用。
当_tmain函数返回后,Mike这个兑现的声明周期结束,它的析构函数被调用。
上述代码中存在一个比较关键的地方,那就是在调用Test函数的时候,涉及到将Mike对象拷贝给Somebody对象。
上述代码的执行结果如下:
我们可以从结果中看到,这次并没有拷贝构造函数被调用,个人猜测,Person(50)这个临时构造的对象就是Somebody这个对象,它们实际上是一个对象,存在于Test函数的栈中,至于这是C++本身的标准还是VS2008帮助我们优化之后的结果,尚未进行研究。
二、函数的返回值是对象
上述代码的执行结果如下:
上述代码的简单解释:
Test函数被执行,Person Jack(30)被执行,构造函数被调用。
Person Tom = Test();Test函数执行完成之后,将Jack对象拷贝给Tom对象,所以,Tom的拷贝构造函数被执行,返回值赋值完成之后,Jack对象的生命周期结束,析构函数被执行,_tmain函数返回之后,Tom对象的生命周期结束,析构函数被执行。
上述代码的执行结果如下:
简单的分析上述代码:
我们看到,代码中并没有定义一个对象来接受Test函数的返回值,但是系统仍然为我们创造了一个临时的对象来接受Test函数的返回值,但是因为这个临时的对象并没有被我们用到,所以,它马上就被析构了。
上述代码的执行结果如下:
我们可以从结果中看到,这次并没有拷贝构造函数被调用,个人猜测,Person(30)这个临时构造的对象就是Tom这个对象,它们实际上是一个对象,存在于_tmain函数的栈中,至于这是C++本身的标准还是VS2008帮助我们优化之后的结果,尚未进行研究。
三、函数的传入参数和返回值都是对象。
上述代码的执行结果如下:
上述代码的简单解释如下:
Person Mike(20);
构造函数被执行
将Mike作为实参传入到Test函数当中,实参到形参的拷贝过程调用了Somebody的拷贝构造函数。
Test函数执行结束,Somebody对象被拷贝给Tom对象,调用了Tom对象的拷贝构造函数。
Somebody的生命周期结束,析构函数执行。
Tom对象的生命周期结束,析构函数被执行。
Mike对象的生命周期结束,析构函数被执行。
我在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对象的生命周期结束,析构函数被执行。
相关文章推荐
- 一个字符串参数(s)由字母(a-z,A-Z)组成, 且最大字符位数为40,要求写一个函数, 返回该参数中连续相同字母的最大个数及该字母,如果最大位数有多个,则返回第一个。例:字符串“aaaddxxxxddddxxxx”,返回值为:“x,4”。
- C++.net 定义类 将类对象作为函数参数传入另一个类 出现错误
- sql语句,oracal更新操作传入参数为对象,判断对象中的字段是否有值,如果有就更新,如果没有就不更新
- c++函数在传入对象参数和返回对象时候的内存调用情况
- 实现 fizzBuzz 函数,参数 num 与返回值的关系如下: 1、如果 num 能同时被 3 和 5 整除,返回字符串 fizzbuzz 2、如果 num 能被 3 整除,返回字符串 fizz 3
- [c++11]如果函数不修改传入的参数,则参数添加const 修饰 -- rvalue
- C语言学习4: 函数返回值与传入参数,关于函数值传递和类型隐性转换,变量不同的作用域,static变量,多文件编译例如两个C文件,显示函数调用语句跳转,递归,斐波那契数列,多文件编译相同变量的问题。
- C函数只能有一个返回值,如果需要返回多个值,怎么办,其实很简单,只要将指针作为函数参数传递就可以了
- javascript函数内设置传入参数对象导致函数外的实参被修改
- Python开发基础-Day6-函数参数、嵌套、返回值、对象、命名空间和作用域
- stl算法设计理念_函数对象和函数对象当参数和返回值
- STL算法设计理念 - 函数对象和函数对象当参数和返回值
- STL函数对象当参数和返回值
- STL算法设计理念 - 函数对象和函数对象当参数和返回值
- C++函数参数传值OR传引用_C++函数返回值返回对象OR对象引用
- C++,当函数参数或者返回值是对象......(★firecat推荐★)
- python基础之函数参数、嵌套、返回值、对象、命名空间和作用域
- stl算法设计理念:函数对象和函数对象当参数和返回值
- 在JavaScript中使用Qt对象成员函数的参数及返回值问题
- COM接口函数参数定义如果缺省[in][out]则默认为[in]