由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - template 类的继承问题
相关主题
find bugs of c++ codestemplate
Help: who has gcc 4.0 or higherCannot use my own container as the underlying container of a stack? (c++)
C++中如何引用模板类中的模板函数一个C++ template的问题
问一个C++的问题请教 C++ std::list iterator 对 template class pointer 的应用问题
C++ templatewhere to define my template function
C++ class template specialization questionthrust help ~~~
请教C++问题boost::function 的 syntax 问题
我这个C++程序有没有什么问题啊?请指点。。。谢谢。。。[合集] Effecive c++ 真那么好? (转载)
相关话题的讨论汇总
话题: numarray话题: tp话题: base话题: template话题: deriv
进入Programming版参与讨论
1 (共1页)
s*****k
发帖数: 604
1
在子类 Deriv中使用父类的数据成员numArray就会出错,
提示的错误信息是numArray没有declare.
改成 Base<_Tp>::numArray 就编译通过了。
请问这是为什么?
我用的编译器是 gcc 3.4.4 (cygwin) 和 gcc 4.2.4 (ubuntu)
#include
template
class Base
{
protected:
_Tp numArray[3];
Base()
{
numArray[0] = static_cast<_Tp>(1);
numArray[1] = static_cast<_Tp>(11);
numArray[2] = static_cast<_Tp>(111);
};
};
template
class Deriv: public Base<_Tp> {
public:
Deriv() {
/* Why the followi
S*********g
发帖数: 5298
2
用this->numArray

【在 s*****k 的大作中提到】
: 在子类 Deriv中使用父类的数据成员numArray就会出错,
: 提示的错误信息是numArray没有declare.
: 改成 Base<_Tp>::numArray 就编译通过了。
: 请问这是为什么?
: 我用的编译器是 gcc 3.4.4 (cygwin) 和 gcc 4.2.4 (ubuntu)
: #include
: template
: class Base
: {
: protected:

s*****k
发帖数: 604
3
有什么原因吗?

【在 S*********g 的大作中提到】
: 用this->numArray
s*****k
发帖数: 604
4
好像不是模板类的话直接用numArray就没什么问题

【在 S*********g 的大作中提到】
: 用this->numArray
E*****7
发帖数: 128
5
Both versions compile and run under Visual Studio 2005.
It would be safer to define the ctor of the deriv as:
deriv() : Base() // explicitly call the ctor of Base
{ ....}
t****t
发帖数: 6806
6
因为编译器不知道到哪里去找numarray
base类是个模板, 在template parameter确定以前, 不能肯定有什么, 所以缺省是不找
base类的. 你必须显式声明numarray 是个dependent name, 才能让编译器暂时忽略这
个找不到的名字.

【在 s*****k 的大作中提到】
: 好像不是模板类的话直接用numArray就没什么问题
s*****k
发帖数: 604
7
刚用borland c++ compiler 编译就没问题。算是g++的bug吗?

【在 s*****k 的大作中提到】
: 在子类 Deriv中使用父类的数据成员numArray就会出错,
: 提示的错误信息是numArray没有declare.
: 改成 Base<_Tp>::numArray 就编译通过了。
: 请问这是为什么?
: 我用的编译器是 gcc 3.4.4 (cygwin) 和 gcc 4.2.4 (ubuntu)
: #include
: template
: class Base
: {
: protected:

t****t
发帖数: 6806
8
why it's safer?

【在 E*****7 的大作中提到】
: Both versions compile and run under Visual Studio 2005.
: It would be safer to define the ctor of the deriv as:
: deriv() : Base() // explicitly call the ctor of Base
: { ....}

t****t
发帖数: 6806
9
恰恰相反, 只有g++在这一点上是正确的

【在 s*****k 的大作中提到】
: 刚用borland c++ compiler 编译就没问题。算是g++的bug吗?
E*****7
发帖数: 128
10
Both versions compile and run under Visual Studio 2005.
相关主题
C++ class template specialization questiontemplate
请教C++问题Cannot use my own container as the underlying container of a stack? (c++)
我这个C++程序有没有什么问题啊?请指点。。。谢谢。。。一个C++ template的问题
进入Programming版参与讨论
t****t
发帖数: 6806
11
VC is notorious in not conforming to C++ standard. g++ is much closer.

【在 E*****7 的大作中提到】
: Both versions compile and run under Visual Studio 2005.
n****y
发帖数: 106
12
这个是effective c++ 的item 43
但是在编译的时候,不是已经知道template parameter了吗?template不是only
compile when needed吗?
给详细讲讲?

【在 t****t 的大作中提到】
: 因为编译器不知道到哪里去找numarray
: base类是个模板, 在template parameter确定以前, 不能肯定有什么, 所以缺省是不找
: base类的. 你必须显式声明numarray 是个dependent name, 才能让编译器暂时忽略这
: 个找不到的名字.

t****t
发帖数: 6806
13
compile有很多步骤的
新一点的的编译器会先检查语法, 做name lookup, dependent name会在name lookup时
忽略

【在 n****y 的大作中提到】
: 这个是effective c++ 的item 43
: 但是在编译的时候,不是已经知道template parameter了吗?template不是only
: compile when needed吗?
: 给详细讲讲?

n****y
发帖数: 106
14
还是不太明白
l**b
发帖数: 1
15
When compiler encounters a template, it does name look up in two steps. For
all types that are non-template dependent, it checks at the time the
template is defined. For all template dependent types, it has to wait until
the template is instantiated to do name loop up. Since your array does not
have any qualifier to tell compiler that it is template type dependent,
compiler assumes it is not and tried to resolve the name look up and
declared error.
That is also why if you add this or Base<_tp>::

【在 n****y 的大作中提到】
: 还是不太明白
1 (共1页)
进入Programming版参与讨论
相关主题
[合集] Effecive c++ 真那么好? (转载)C++ template
模板对象能不能作为成员变量使用C++ class template specialization question
One c++ non-type template question请教C++问题
这个是什么设计模式?我这个C++程序有没有什么问题啊?请指点。。。谢谢。。。
find bugs of c++ codestemplate
Help: who has gcc 4.0 or higherCannot use my own container as the underlying container of a stack? (c++)
C++中如何引用模板类中的模板函数一个C++ template的问题
问一个C++的问题请教 C++ std::list iterator 对 template class pointer 的应用问题
相关话题的讨论汇总
话题: numarray话题: tp话题: base话题: template话题: deriv