A
alan
Hello all, I'm wondering if it's valid for a templated class to use a
template parameter as a base class.
Basically I have two abstract types, one of which is derived from the
other, and I have an implementation for those types. Note however
that their implementations are practically the same, only the two
abstract types are different only in the bit that is derived from the
other:
class Closure {
public:
virtual int& operator[](size_t i) =0;
virtual int const& operator[](size_t i) const =0;
};
class KClosure: public Closure {
public:
bool reusable;
void banreuse(){
reusable = 0;
}
KClosure() : reusable(1), Closure() {}
//KClosure doesn't add any virtual functions,
//but does add a few non-virtuals
};
Now what I tried in g++ is:
template<typename T, size_t N>
class ClosureArray : public T{
int dat[N];
public:
ClosureArray<T,N>() : T() {}
virtual int& operator[](size_t i){
return dat;
}
virtual int const& operator[](size_t i) const{
return dat;
}
}
g++ compiled the above in what seems to be what I expected it to work
(although I haven't done a very comprehensive test).
What I'd like to know is whether this is legal standard C++ and is
portable to a reasonably large number of non-g++ compilers.
template parameter as a base class.
Basically I have two abstract types, one of which is derived from the
other, and I have an implementation for those types. Note however
that their implementations are practically the same, only the two
abstract types are different only in the bit that is derived from the
other:
class Closure {
public:
virtual int& operator[](size_t i) =0;
virtual int const& operator[](size_t i) const =0;
};
class KClosure: public Closure {
public:
bool reusable;
void banreuse(){
reusable = 0;
}
KClosure() : reusable(1), Closure() {}
//KClosure doesn't add any virtual functions,
//but does add a few non-virtuals
};
Now what I tried in g++ is:
template<typename T, size_t N>
class ClosureArray : public T{
int dat[N];
public:
ClosureArray<T,N>() : T() {}
virtual int& operator[](size_t i){
return dat;
}
virtual int const& operator[](size_t i) const{
return dat;
}
}
g++ compiled the above in what seems to be what I expected it to work
(although I haven't done a very comprehensive test).
What I'd like to know is whether this is legal standard C++ and is
portable to a reasonably large number of non-g++ compilers.