博客
关于我
强烈建议你试试无所不能的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/

你可能感兴趣的文章
响应式图片(自适应图片)
查看>>
Weex系列(6) —— web组件和webview
查看>>
《前端十年心路-我把一切告诉你》的书稿大纲&问题收集
查看>>
理解async/await
查看>>
极致的性能
查看>>
前端代码的错误日志收集了解一下
查看>>
【vuex】理解Getters
查看>>
Thinking——斐波拉契数列不用递归
查看>>
tideways+toolkit对php代码进行性能分析
查看>>
10 个常用的软件架构模式
查看>>
Windows Containers 大冒险: 术语
查看>>
大厂前端面试考什么?
查看>>
重构 Composer 源管理工具 CRM 为 composer 插件
查看>>
Linux中的阻塞机制及等待队列
查看>>
我对JS字典的简单学习
查看>>
[译]HTML attribute与DOM property之间的区别?
查看>>
如何用Uber JVM Profiler等可视化工具监控Spark应用程序?
查看>>
Unix高级环境编程
查看>>
揭秘天猫未来小店背后的NEC the WISE技术群
查看>>
CentOS 6.5上安装并配置FastDFS
查看>>