Re: [語法] template和繼承的寫法
※ 引述《nickexe (nick.exe)》之銘言:
: 改成 this->parameterA; 就可以了,
: 這問題應該是編譯器的關係,
: 至於為什麼你可以參考這篇
: http://tinyurl.com/nr6r62
: Dev C++ 內建的 MinGW 剛好是 GCC 3.4.x 的版本.
這絕對不是什麼編譯器的問題,
是本來應該要這樣,
C++ Primer 3/e 就有概略提過了,
Effective C++ 3/e 還特地整理一條出來講:
Item 43: Know how to access names in templatized base classes.
主要是在說 compiler 編譯 classB 時並不知道它繼承的是什麼 class,
因為只看到 class classB : class classA<T> 而不知道 T 是什麼東西,
所以完全無從判斷 classA<T> 應該長得像什麼樣子;
就算已經看見 classA 這個 class template 的定義式好了,
因為 class 支援 total template specialization,
你高興的話甚至可以針對不同的 T 做出完全長得不一樣的東西來,
所以 compiler 是猜不透它的。
方法除了 this->xxxx 外,
也能在 class 定義式裡用 using classA<T>::xxxx 上提,
或是明確寫出 classA<T>::xxxx,
雖然 Effective C++ 3/e 舉的例子是 member function,
原 po 問的是 data member,
不過基本原則還是差不多,
這些做法 C++ Primer 3/e 也提過,
4/e 不確定有沒有。
--
Ling-hua Tseng (uranus@tinlans.org)
Department of Computer Science, National Tsing-Hua University
Interesting: C++, Compiler, PL/PD, OS, VM, Large-scale software design
Researching: Software pipelining for VLIW architectures
Homepage: https://www.tinlans.org
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.160.109.33
→
09/06 07:32, , 1F
09/06 07:32, 1F
推
06/01 21:49, , 2F
06/01 21:49, 2F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 3 之 3 篇):
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章