K******g 发帖数: 1870 | 1 今天面试被问到template class怎么处理Inheritance问题,我没有答上来,觉得
template class处理inheritance应该与普通的class没有什么区别。
放下电话后,在effective C++的item43里找到了,derived class cannot inherit
public functions from the base class, we need use this->, or using to use
public functions from the base class in the derived class.
但是,我写了个程序, derived class still can inherit public functions from
the base class.(用的是g++编译器)
请问C++的大牛们,到底哪个对啊?难道这种东西是compiler dependent的?
还有这个面试题应该怎么回答呢?多谢了 | I*****y 发帖数: 602 | 2 template class本身和成员函数只有在调用的时候才被instantiation, 在你的main函
数里面调用一下你的derived class的成员看看。
template class即使本身逻辑有错,只要不被instantiation的话,编译器也不会报错
。编译器只看语法。
【在 K******g 的大作中提到】 : 今天面试被问到template class怎么处理Inheritance问题,我没有答上来,觉得 : template class处理inheritance应该与普通的class没有什么区别。 : 放下电话后,在effective C++的item43里找到了,derived class cannot inherit : public functions from the base class, we need use this->, or using to use : public functions from the base class in the derived class. : 但是,我写了个程序, derived class still can inherit public functions from : the base class.(用的是g++编译器) : 请问C++的大牛们,到底哪个对啊?难道这种东西是compiler dependent的? : 还有这个面试题应该怎么回答呢?多谢了
| P*******e 发帖数: 65 | 3 If the function inherited from the base template class has paramters as the
type of the base template class, the "this->" can be avoided. Otherwise, "
this->" is necessary to tell the compiler the type used in the base template.
template
class BaseClass {
public:
BaseClass() {};
~BaseClass() {};
void Write(T var);
T Read(void);
private:
T variable;
};
template
class ChildClass : public BaseClass {
public:
ChildClass() {};
~ChildClass() {};
void WriteA
【在 K******g 的大作中提到】 : 今天面试被问到template class怎么处理Inheritance问题,我没有答上来,觉得 : template class处理inheritance应该与普通的class没有什么区别。 : 放下电话后,在effective C++的item43里找到了,derived class cannot inherit : public functions from the base class, we need use this->, or using to use : public functions from the base class in the derived class. : 但是,我写了个程序, derived class still can inherit public functions from : the base class.(用的是g++编译器) : 请问C++的大牛们,到底哪个对啊?难道这种东西是compiler dependent的? : 还有这个面试题应该怎么回答呢?多谢了
|
|