博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(原創) unnamed object的多型只能使用reference (C/C++)
阅读量:7187 次
发布时间:2019-06-29

本文共 1875 字,大约阅读时间需要 6 分钟。

當使用unnamed object且須多型時,只能使用 reference,若用pointer雖可compile,但結果不正確。

以下是使用reference配合unnamed object使用多型,結果正確

 1
ExpandedBlockStart.gif
ContractedBlock.gif
/**/
/* 
 2InBlock.gif(C) OOMusou 2007 http://oomusou.cnblogs.com
 3InBlock.gif
 4InBlock.gifFilename    : Polymorphism_UnnameObject_reference.cpp
 5InBlock.gifCompiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 6InBlock.gifDescription : Demo how to use reference with unnamed object
 7InBlock.gifRelease     : 04/07/2007 1.0
 8ExpandedBlockEnd.gif*/
 9
None.gif#include 
<
iostream
>
10
None.gif#include 
<
string
>
11
None.gif
12
None.gif
using
 
namespace
 std;
13
None.gif
14
ExpandedBlockStart.gifContractedBlock.gif
class
 Base 
dot.gif
{
15InBlock.gifprotected:
16InBlock.gif  string text;
17InBlock.gif  
18InBlock.gifpublic:
19ExpandedSubBlockStart.gifContractedSubBlock.gif  Base() dot.gif{}
20ExpandedSubBlockStart.gifContractedSubBlock.gif  Base(const char* text) : text(text) dot.gif{}
21InBlock.gif  
22InBlock.gifpublic:
23ExpandedSubBlockStart.gifContractedSubBlock.gif  virtual string getText() dot.gif{
24InBlock.gif    return "Base's " + this->text;
25ExpandedSubBlockEnd.gif  }
26ExpandedBlockEnd.gif}
;
27
None.gif
28
ExpandedBlockStart.gifContractedBlock.gif
class
 Derived : 
public
 Base 
dot.gif
{
29InBlock.gifpublic:
30ExpandedSubBlockStart.gifContractedSubBlock.gif  Derived() dot.gif{}
31ExpandedSubBlockStart.gifContractedSubBlock.gif  Derived(const char *text) : Base(text) dot.gif{}
32InBlock.gif  
33InBlock.gifpublic:
34ExpandedSubBlockStart.gifContractedSubBlock.gif  string getText() dot.gif{
35InBlock.gif    return "Derived's " + this->text;
36ExpandedSubBlockEnd.gif  }
37ExpandedBlockEnd.gif}
;
38
None.gif
39
ExpandedBlockStart.gifContractedBlock.gif
int
 main() 
dot.gif
{
40InBlock.gif  Base &obj = Derived("C++");
41InBlock.gif  cout << obj.getText() << endl;
42ExpandedBlockEnd.gif}

執行結果

None.gif
Derived's C++

但若換成pointer,則結果錯誤

 1
ExpandedBlockStart.gif
ContractedBlock.gif
/**/
/* 
 2InBlock.gif(C) OOMusou 2007 http://oomusou.cnblogs.com
 3InBlock.gif
 4InBlock.gifFilename    : Polymorphism_UnnameObject_pointer.cpp
 5InBlock.gifCompiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 6InBlock.gifDescription : Demo how to use pointer with unnamed object
 7InBlock.gifRelease     : 04/07/2007 1.0
 8ExpandedBlockEnd.gif*/
 9
None.gif#include 
<
iostream
>
10
None.gif#include 
<
string
>
11
None.gif
12
None.gif
using
 
namespace
 std;
13
None.gif
14
ExpandedBlockStart.gifContractedBlock.gif
class
 Base 
dot.gif
{
15InBlock.gifprotected:
16InBlock.gif  string text;
17InBlock.gif  
18InBlock.gifpublic:
19ExpandedSubBlockStart.gifContractedSubBlock.gif  Base() dot.gif{}
20ExpandedSubBlockStart.gifContractedSubBlock.gif  Base(const char* text) : text(text) dot.gif{}
21InBlock.gif  
22InBlock.gifpublic:
23ExpandedSubBlockStart.gifContractedSubBlock.gif  virtual string getText() dot.gif{
24InBlock.gif    return "Base's " + this->text;
25ExpandedSubBlockEnd.gif  }
26ExpandedBlockEnd.gif}
;
27
None.gif
28
ExpandedBlockStart.gifContractedBlock.gif
class
 Derived : 
public
 Base 
dot.gif
{
29InBlock.gifpublic:
30ExpandedSubBlockStart.gifContractedSubBlock.gif  Derived() dot.gif{}
31ExpandedSubBlockStart.gifContractedSubBlock.gif  Derived(const char *text) : Base(text) dot.gif{}
32InBlock.gif  
33InBlock.gifpublic:
34ExpandedSubBlockStart.gifContractedSubBlock.gif  string getText() dot.gif{
35InBlock.gif    return "Derived's " + this->text;
36ExpandedSubBlockEnd.gif  }
37ExpandedBlockEnd.gif}
;
38
None.gif
39
ExpandedBlockStart.gifContractedBlock.gif
int
 main() 
dot.gif
{
40InBlock.gif  Base *obj = &Derived("C++");
41InBlock.gif  cout << obj->getText() << endl;
42ExpandedBlockEnd.gif}

執行結果

None.gif
Derived's

Conclusion
為什麼會如此,我並不清楚,目前只知道unnamed object若要配合多型,一定要用reference,不能用pointer,若有人知道原因請告訴我,謝謝。

转载地址:http://hhukm.baihongyu.com/

你可能感兴趣的文章
MongoDB安全及身份认证
查看>>
oc精简笔记
查看>>
python的多线程和守护线程
查看>>
traditional:true
查看>>
PS字体加粗的小方法、、
查看>>
构造水题 Codeforces Round #206 (Div. 2) A. Vasya and Digital Root
查看>>
友元程序集
查看>>
Mysql表编辑
查看>>
规定密码以字母开头只能包含字母、数字和下划线
查看>>
计数排序 + 线段树优化 --- Codeforces 558E : A Simple Task
查看>>
maven下载及安装
查看>>
svn安装
查看>>
catalog
查看>>
第1章 MATLAB概述
查看>>
检查是否存在工艺路线
查看>>
内存分配
查看>>
Codeforces Round #298 (Div. 2) D. Handshakes [贪心]
查看>>
gridview单元格中获取行索引。。。
查看>>
数据中约束条件
查看>>
jmeter查看结果树的响应数据乱码解决办法
查看>>