Re: Template inheritance

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jonathan Wakely wrote:
You could write this->FBase instead.
I strongly dislike that approach. I guess that one is a matter of personal preference. But let me suggest a different approach:

template<typename T>
class B : public A<T>
{
public:
  typedef A<T> super;
  using super::FBase;
  typedef typename super::some_type some_type;


1) Even when I don't have the template inheritance problem, I like to typedef the major base class of each class with the name super (the name is copied from a feature of java that I like).  There are many situations in which a class explicitly refers to its base class.  Then there are many situations in which a new layer gets inserted in a class hierarchy as a project evolves.  If you use super instead of the actual major base class, then all the explicit references are covered across that change in class hierarchy by changing one typedef.  Also base class name in template programming tend to be very verbose.  I prefer using that long name just twice at the top of the class and using super everywhere else, rather than use the long name everywhere.

2) The actual answer to icegood's question.  The using statement tells the compiler that a specific member (function or data) is inherited from or through that base class.  You can do that once in the class definition rather than do the this-> thing on every use of the inherited name.

Note that a single using carries all the way down to where the name is defined.  If a name is defined in X<T> which is a base class of Y<Y> which is a base class of Z<T> and that name is used in Z but not in Y, you only need the using statement in Z, not in Y.

template<typename T>
class Z : public Y<T>
{ public:
  typedef Y<T> super;
  using super::some_name_from_X;

Even though that name isn't usable in Y (without a using statement in Y) it can be used in Z after telling the compiler it is inherited through Y (you don't need to tell the compiler it comes from X).

3) So far as I understand, you can't inherit types that way:
  using typename super::some_type;
so I use a typedef to define a name equivalent to the inherited type.
  typedef typename super::some_type some_type;




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux