Posted on 2006-03-30 10:34
Raistlin 阅读(4143)
评论(0) 编辑 收藏 引用 所属分类:
C++
class
CTestCopyConstruct
{
public
:
CTestCopyConstruct()
{
TRACE(
"
Enter CTestCopyConstruct();this is %d\n
"
,
this
);
strTest
=
"
not ok
"
;
i
=
0
;
}
CTestCopyConstruct(
const
CTestCopyConstruct
&
src)
{
TRACE(
"
Enter CTestCopyConstruct(const CTestCopyConstruct &src);this is %d;src is %d\n
"
,
this
,
&
src);
strTest
=
src.strTest;
i
=
src.i;
}
CTestCopyConstruct
&
operator
=
(
const
CTestCopyConstruct
&
src)
{
TRACE(
"
Enter CTestCopyConstruct & operator =(const CTestCopyConstruct & src);this is %d;src is %d\n
"
,
this
,
&
src);
strTest
=
src.strTest;
i
=
src.i;
return
*
this
;
}
CString strTest;
int
i;
}
;
CTestCopyConstruct GetTest()
{
CTestCopyConstruct ret1;
ret1.strTest
=
"
ok
"
;
ret1.i
=
0
;
CTestCopyConstruct ret2;
return
ret1;
}
void
CTestDlg::OnOK()
{
CTestCopyConstruct var1;
CTestCopyConstruct var2
=
GetTest();
TRACE(
"
\nresult 1:\n
"
);
TRACE(
"
var1 is %d\n
"
,
&
var1);
TRACE(
"
var2 is %d var2.str is %s\n\n
"
,
&
var2,var2.strTest);
CTestCopyConstruct var3
=
var2;
CTestCopyConstruct var4;
var4
=
var2;
TRACE(
"
\nresult 2:\n
"
);
TRACE(
"
var3 is %d var3.str is %s\n
"
,
&
var3,var3.strTest);
TRACE(
"
var4 is %d var2.str is %s\n
"
,
&
var4,var4.strTest);
}
代码如上,调试窗口输出如下:
Enter CTestCopyConstruct();this is 1242980
Enter CTestCopyConstruct();this is 1242848
Enter CTestCopyConstruct();this is 1242840
Enter CTestCopyConstruct(const CTestCopyConstruct &src);this is 1242972;src is 1242848
result 1:
var1 is 1242980
var2 is 1242972 var2.str is ok
Enter CTestCopyConstruct(const CTestCopyConstruct &src);this is 1242964;src is 1242972
Enter CTestCopyConstruct();this is 1242956
Enter CTestCopyConstruct & operator =(const CTestCopyConstruct & src);this is 1242956;src is 1242972
result 2:
var3 is 1242964 var3.str is ok
var4 is 1242956 var2.str is ok
分析:
CTestCopyConstruct var1;\\1
CTestCopyConstruct var2 = GetTest();\\2
代码的执行如下:
当前堆栈指针(sp) = 1242980
sp -= 8//在堆栈中为var1分配空间
在var1上(1242980 - 1242973)调用构造函数
sp -= 8//在堆栈中为var2分配空间
sp -= n//保护当前环境
进入了GetTest函数
当前sp = 1242848
sp -= 8//为ret1分配空间
构建ret1
sp -= 8//为ret2分配空间
构建ret2
......
对var2(1242972处的堆栈段)调用拷贝构造函数,以test1(1242848处)为参数
//析构test1 test2等...
sp += n//恢复运行环境
......
另:
operater = () 和默认构造函数不一样,只重写=运算符而不提供拷贝构造函数,调用的仍然是默认的构造函数。
默认构造函数和赋值运算符处理的情况不一样,一个是在已分配的空间上调用,一个是在已构造的对象上调用。
默认拷贝构造函数会调用类中各成员的拷贝构造函数。CString 由于提供了拷贝构造函数,所以上面例子中即使去掉拷贝构造函数,var2 仍然会得到正确的值。
调试的环境是vc6.0 debug 默认选项。编译没有优化。
CTestCopyConstruct(
const
CTestCopyConstruct
&
src)
{
TRACE(
"
Enter CTestCopyConstruct(const CTestCopyConstruct &src);this is %d;src is %d\n
"
,
this
,
&
src);
strTest
=
src.strTest;
i
=
src.i;
}
与
CTestCopyConstruct(
const
CTestCopyConstruct
&
src)
:strTest (src.strTest)
{
TRACE(
"
Enter CTestCopyConstruct(const CTestCopyConstruct &src);this is %d;src is %d\n
"
,
this
,
&
src);
i
=
src.i;
}
前者先调用了CString::CString()再调用CString::operator =
后者直接调用了CString::CString(CString & src);
默认的赋值运算的行为:首先调用父类的赋值运算。
然后会为自己独有的各成员寻找赋值运算。如果成员的赋值运算符被重写,则调用这个重写的赋值运算符函数,如果这个重写的运算符函数是private,编译将无法通过。
默认的拷贝构造函数的行为:首先调用父类的拷贝构造函数。
然后为自己独有的各成员寻找拷贝构造函数。如果这个成员提供拷贝构造函数,则调用之,如果成员的类提供的拷贝构造函数是private,编译将无法通过。
(子类完全可以把父类当成自己的一个成员?)
可以说默认的赋值运算和默认的拷贝构造函数是类最常被用到的两个函数了...内部却不是一般的复杂。