📄 condesafterinheritance.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="css/stdlayout.css" type="text/css">
<link rel="stylesheet" href="css/print.css" type="text/css">
<meta content="text/html; charset=gb2312" http-equiv="content-type">
<title>继承后的建构函式与解构函式</title>
</head>
<body>
<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>
<h1><a href="CppGossip.html">C++
Gossip: 继承后的建构函式与解构函式</a></h1>
若您继承了某个类别之后,当您在生成衍生类别的物件时若不指定参数,无参数的预设建构子会被执行,而基础类别的无参数预设建构子也会被执行,所以基于这种
特性,通常预设建构子中会撰写一些通用的成员状态初始,例如设定一些预设值。 <br>
<br>
如果继承之后,您要使用衍生类别生成物件,在生成物件时指定参数,并同时执行基底类别中的某个参数建构子,您可以使用 <span style="font-weight: bold;">: </span>运算子,例如: <br>
<div style="margin-left: 40px; font-family: Courier New,Courier,monospace;"><span style="font-weight: bold;">// Point2D类别 </span><br style="font-weight: bold;">
<span style="font-weight: bold;">class Point2D { </span><br style="font-weight: bold;">
<span style="font-weight: bold;">public: </span><br style="font-weight: bold;">
<span style="font-weight: bold;">
Point2D() {</span><br style="font-weight: bold;">
<span style="font-weight: bold;">
_x = 0; </span><br style="font-weight: bold;">
<span style="font-weight: bold;">
_y = 0;</span><br style="font-weight: bold;">
<span style="font-weight: bold;"> } </span><br style="font-weight: bold;">
<span style="font-weight: bold;">
Point2D(int x, int y) : _x(x), _y(y) {</span><br style="font-weight: bold;">
<span style="font-weight: bold;"> }</span><br style="font-weight: bold;">
<span style="font-weight: bold;"> </span><br style="font-weight: bold;">
<span style="font-weight: bold;">private:</span><br style="font-weight: bold;">
<span style="font-weight: bold;"> int
_x;</span><br style="font-weight: bold;">
<span style="font-weight: bold;"> int
_y; </span><br style="font-weight: bold;">
<span style="font-weight: bold;">}; </span><br style="font-weight: bold;">
<br style="font-weight: bold;">
<span style="font-weight: bold;">// Point3D类别 </span><br style="font-weight: bold;">
<span style="font-weight: bold;">class Point3D :
public Point2D { // 继承Point2D类别 </span><br style="font-weight: bold;">
<span style="font-weight: bold;">public: </span><br style="font-weight: bold;">
<span style="font-weight: bold;">
Point3D() { </span><br style="font-weight: bold;">
<span style="font-weight: bold;">
_z = 0; </span><br style="font-weight: bold;">
<span style="font-weight: bold;"> } </span><br style="font-weight: bold;">
<br style="font-weight: bold;">
<span style="font-weight: bold;"> //
建构函式,同时指定呼叫父类别建构函式 </span><br style="font-weight: bold;">
<span style="font-weight: bold;">
Point3D(int x, int y, int z) : Point2D(x, y), _z(z) { </span><br style="font-weight: bold;">
<span style="font-weight: bold;"> } </span><br style="font-weight: bold;">
<br style="font-weight: bold;">
<span style="font-weight: bold;">private:</span><br style="font-weight: bold;">
<span style="font-weight: bold;"> int
_z; // 新增私用资料 </span><br style="font-weight: bold;">
<span style="font-weight: bold;">};</span><br>
</div>
<br>
<br>
如果您使用衍生类别生成物件,则建构函式的执行顺序会从基底类别的建构函式开始执行起,这是可以理解的,因为基底类别是衍生类别的基础,一些基础的参数或
初始状态必须先完成,再来再完成衍生类别中的建构函式。 <br>
<br>
而在物件被消灭时,解构函式的执行顺序则正好相反,是从衍生类别的解构函式开始执行,再来才是基础类别的建构函式,因为若基底类别的解构函式如果先执行,
则衍生类别相依于基底类别的一些状态也会被解构(例如指标),则此时再行衍生类别的解构函式,将存在着相依问题而造成错误。 <br>
<br>
下面这个简单的程式可以告诉您建构函式与解构函式,在继承之后的执行顺序: <br>
<br>
<pre>#include <iostream> <br>using namespace std; <br><br>class Foo1 { <br>public: <br> Foo1() { <br> cout << "Foo1建构函式" << endl; <br> } <br><br> ~Foo1() { <br> cout << "Foo1解构函式" << endl; <br> } <br>}; <br><br>class Foo2 : public Foo1 { <br>public: <br> Foo2() { <br> cout << "Foo2建构函式" << endl; <br> } <br><br> ~Foo2() { <br> cout << "Foo2解构函式" << endl; <br> } <br>}; <br><br>int main() { <br> Foo2 f; <br><br> cout << endl; <br> <br> return 0;<br>}</pre>
<span class="postbody"><br>
执行结果:</span><br>
<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="background-color: rgb(0, 0, 0);"><small><span style="color: rgb(255, 255, 255);">Foo1建构函式<br>
Foo2建构函式<br>
<br>
Foo2解构函式<br>
Foo1解构函式</span></small><span style="color: rgb(255, 255, 255);"><br>
</span></td>
</tr>
</tbody>
</table>
<br>
<br>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -