(原創) unnamed object的多型只能使用reference (C/C++)
當使用unnamed object且須多型時,只能使用 reference,若用pointer雖可compile,但結果不正確。
以下是使用reference配合unnamed object使用多型,結果正確
1/*
2(C) OOMusou 2007 http://oomusou.cnblogs.com
3
4Filename : Polymorphism_UnnameObject_reference.cpp
5Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
6Description : Demo how to use reference with unnamed object
7Release : 04/07/2007 1.0
8*/
9#include <iostream>
10#include <string>
11
12using namespace std;
13
14class Base {
15protected:
16 string text;
17
18public:
19 Base() {}
20 Base(const char* text) : text(text) {}
21
22public:
23 virtual string getText() {
24 return "Base's " + this->text;
25 }
26};
27
28class Derived : public Base {
29public:
30 Derived() {}
31 Derived(const char *text) : Base(text) {}
32
33public:
34 string getText() {
35 return "Derived's " + this->text;
36 }
37};
38
39int main() {
40 Base &obj = Derived("C++");
41 cout << obj.getText() << endl;
42}
2(C) OOMusou 2007 http://oomusou.cnblogs.com
3
4Filename : Polymorphism_UnnameObject_reference.cpp
5Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
6Description : Demo how to use reference with unnamed object
7Release : 04/07/2007 1.0
8*/
9#include <iostream>
10#include <string>
11
12using namespace std;
13
14class Base {
15protected:
16 string text;
17
18public:
19 Base() {}
20 Base(const char* text) : text(text) {}
21
22public:
23 virtual string getText() {
24 return "Base's " + this->text;
25 }
26};
27
28class Derived : public Base {
29public:
30 Derived() {}
31 Derived(const char *text) : Base(text) {}
32
33public:
34 string getText() {
35 return "Derived's " + this->text;
36 }
37};
38
39int main() {
40 Base &obj = Derived("C++");
41 cout << obj.getText() << endl;
42}
執行結果
Derived's C++
但若換成pointer,則結果錯誤
1/*
2(C) OOMusou 2007 http://oomusou.cnblogs.com
3
4Filename : Polymorphism_UnnameObject_pointer.cpp
5Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
6Description : Demo how to use pointer with unnamed object
7Release : 04/07/2007 1.0
8*/
9#include <iostream>
10#include <string>
11
12using namespace std;
13
14class Base {
15protected:
16 string text;
17
18public:
19 Base() {}
20 Base(const char* text) : text(text) {}
21
22public:
23 virtual string getText() {
24 return "Base's " + this->text;
25 }
26};
27
28class Derived : public Base {
29public:
30 Derived() {}
31 Derived(const char *text) : Base(text) {}
32
33public:
34 string getText() {
35 return "Derived's " + this->text;
36 }
37};
38
39int main() {
40 Base *obj = &Derived("C++");
41 cout << obj->getText() << endl;
42}
2(C) OOMusou 2007 http://oomusou.cnblogs.com
3
4Filename : Polymorphism_UnnameObject_pointer.cpp
5Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
6Description : Demo how to use pointer with unnamed object
7Release : 04/07/2007 1.0
8*/
9#include <iostream>
10#include <string>
11
12using namespace std;
13
14class Base {
15protected:
16 string text;
17
18public:
19 Base() {}
20 Base(const char* text) : text(text) {}
21
22public:
23 virtual string getText() {
24 return "Base's " + this->text;
25 }
26};
27
28class Derived : public Base {
29public:
30 Derived() {}
31 Derived(const char *text) : Base(text) {}
32
33public:
34 string getText() {
35 return "Derived's " + this->text;
36 }
37};
38
39int main() {
40 Base *obj = &Derived("C++");
41 cout << obj->getText() << endl;
42}
執行結果
Derived's
Conclusion
為什麼會如此,我並不清楚,目前只知道unnamed object若要配合多型,一定要用reference,不能用pointer,若有人知道原因請告訴我,謝謝。
【推荐】FFA 2024大会视频回放:Apache Flink 的过去、现在及未来
【推荐】中国电信天翼云云端翼购节,2核2G云服务器一口价38元/年
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步