(原創) unnamed object的多型只能使用reference (C/C++)

本文探讨了在C++中,当使用匿名对象实现多型时,必须使用引用而非指针的原因。通过对比实例,展示了使用引用时多型行为正确,而使用指针则可能导致错误结果。

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

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


 1ExpandedBlockStart.gifContractedBlock.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*/

 9None.gif#include <iostream>
10None.gif#include <string>
11None.gif
12None.gifusing namespace std;
13None.gif
14ExpandedBlockStart.gifContractedBlock.gifclass 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}
;
27None.gif
28ExpandedBlockStart.gifContractedBlock.gifclass 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}
;
38None.gif
39ExpandedBlockStart.gifContractedBlock.gifint main() dot.gif{
40InBlock.gif  Base &obj = Derived("C++");
41InBlock.gif  cout << obj.getText() << endl;
42ExpandedBlockEnd.gif}


執行結果

None.gifDerived's C++


但若換成pointer,則結果錯誤

 1ExpandedBlockStart.gifContractedBlock.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*/

 9None.gif#include <iostream>
10None.gif#include <string>
11None.gif
12None.gifusing namespace std;
13None.gif
14ExpandedBlockStart.gifContractedBlock.gifclass 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}
;
27None.gif
28ExpandedBlockStart.gifContractedBlock.gifclass 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}
;
38None.gif
39ExpandedBlockStart.gifContractedBlock.gifint main() dot.gif{
40InBlock.gif  Base *obj = &Derived("C++");
41InBlock.gif  cout << obj->getText() << endl;
42ExpandedBlockEnd.gif}


執行結果

None.gifDerived's


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值