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

你可能感兴趣的文章
Java ConcurrentModificationException异常原因和解决方法[转载]
查看>>
单例模式防止反射和反序列化
查看>>
聚集索引和非聚集索引的区别
查看>>
搭建前端监控系统(备选)用户行为统计和监控篇(如何快速定位线上问题)...
查看>>
linux常用命令
查看>>
python获取系统时间
查看>>
frame与bounds的区别比较
查看>>
<正则吃饺子> :关于使用pd创建表时需要注意的地方
查看>>
Python输入和输出
查看>>
GIL(全局解释器锁)
查看>>
sqlserver 计算数据库时间差
查看>>
SQL 存储过程使用
查看>>
Gradle 配置国内镜像
查看>>
php实现排列组合
查看>>
Hibernate入门第二课 Hibernate Tools for Eclipse Plugins安装
查看>>
Redis配置文件详解
查看>>
python学习day4之路文件的序列化和反序列化
查看>>
ArrayList和LinkedList区别及性能测试
查看>>
高精度模板
查看>>
mysql5.7 多级主从+multisource
查看>>