作者tinlans ( )
看板C_and_CPP
标题Re: [语法] template和继承的写法
时间Sat Sep 5 11:32:26 2009
※ 引述《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 (
[email protected])
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
1F:→ waneblade:谢谢你喔 该找时间念一下 09/06 07:32
2F:推 freeman921:soga ! 06/01 21:49