文章来源:http://blog.chinaunix.net/uid-20816252-id-2791683.html
如果函数的传入参数和返回值是对象,那么这个过程中会发生哪些我们未曾注意过的细节呢?
- #include
- #include
- 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;
- }
将Mike作为Test函数的实参传入,Person Somebody的复制构造函数被调用。
- #include
- #include
- 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;
- }
- #include
- #include
- 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;
- }
- #include
- #include
- 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;
- }
- #include
- #include
- 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;
- }
- #include
- #include
- 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;
- }
#include <iostream>
using namespace std;
class Node
{
public:
Node()
{
cout << "Node1()\n";
}
//使用默认的拷贝构造函数
int data1;
int data2;
};
void Fn(Node n)
{
cout << n.data1;
cout << ",";
cout << n.data2;
cout << ",";
n.data1 = 3;
n.data2 = 3;
}
int _tmain(int argc, _TCHAR* argv[])
{
Node n;
n.data1 = 1;
n.data2 = 1;
Fn(n);
cout << n.data1;
cout << ",";
cout << n.data2;
cout << endl;
return 0;
}
运行结果:
Node1()
1,1,1,1
请按任意键继续. . .
#include "stdafx.h"
#include <iostream>
using namespace std;
class Node
{
public:
Node()
{
cout << "Node1()\n";
}
//使用自定义的拷贝构造函数
Node(Node& n);
int data1;
int data2;
};
Node::Node(Node &n)
{
cout << "Node2()\n";
}
void Fn(Node n)
{
cout << n.data1;
cout << ",";
cout << n.data2;
cout << ",";
n.data1 = 3;
n.data2 = 3;
}
int _tmain(int argc, _TCHAR* argv[])
{
Node n;
n.data1 = 1;
n.data2 = 1;
Fn(n);
cout << n.data1;
cout << ",";
cout << n.data2;
cout << endl;
return 0;
}
运行结果:
Node1()
Node2()
1245032,4266166,1,1
请按任意键继续. . .
总结:
类对象做为函数参数时,是被存放在栈上的,不影响实参的数据。
若未重写拷贝构造函数,类的其它构造函数将不会被调用。形参的数据是通过内存拷贝传递的。若重写了,拷贝构造函数将会在初始化形参时被调用,不再进行内存拷贝工作。
firecat实例2(函数返回值是对象)
#include "stdafx.h"
#include <iostream>
using namespace std;
class Node
{
public:
Node()
{
cout << "Node1()\n";
}
//使用默认的拷贝构造函数
int data1;
};
Node Fn()
{
Node n;
n.data1 = 1;
return n;
}
int _tmain(int argc, _TCHAR* argv[])
{
Node f = Fn();
cout << f.data1;
cout << endl;
return 0;
}
运行结果:
Node1()
1
请按任意键继续. . .
#include "stdafx.h"
#include <iostream>
using namespace std;
class Node
{
public:
Node()
{
cout << "Node1()\n";
}
//使用自定义的拷贝构造函数
Node(Node& n)
{
cout << "Node2()\n";
}
int data1;
};
Node Fn()
{
Node n;
n.data1 = 1;
return n;
}
int _tmain(int argc, _TCHAR* argv[])
{
Node f = Fn();
cout << f.data1;
cout << endl;
return 0;
}
运行结果:
Node1()
Node2()
-858993460
请按任意键继续. . .
---------------------
参考文章: