Templates and inheritance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Isaac Gelado

    Templates and inheritance

    Hi,
    I am having problems with inheritance in templates classes. Say I
    have the following classes:

    class A {};
    class B: public A {};
    template<typena me Tclass C {};

    Now in my code I have something like:

    C<A **myAPtr;
    myAPtr = new C<B *>();

    I'm getting an error saying that conversion from C<B *to C<A *is
    not possible. Does it mean I can no use subclassing with templates
    without using ugly casts?

    Thanks,
    Isaac
  • Maxim Yegorushkin

    #2
    Re: Templates and inheritance

    On Oct 16, 3:35 pm, Isaac Gelado <igel...@gmail. comwrote:
     I am having problems with inheritance in templates classes. Say I
    have the following classes:
    >
    class A {};
    class B: public A {};
    template<typena me Tclass C {};
    >
    Now in my code I have something like:
    >
    C<A **myAPtr;
    myAPtr = new C<B *>();
    >
    I'm getting an error saying that conversion from C<B *to C<A *is
    not possible.
    The actual error is that C<B*>* can not be converted to C<A*>*.

    This is expected, as each instantiation of a class template produces a
    distinct class, unless template arguments are the same.
    Does it mean I can no use subclassing with templates
    without using ugly casts?
    To make derived-to-base conversion from C<B>* from C<A>* work C<Bhas
    to derive from C<A>.

    If, on the other hand, you just want to make a conversion from C<Bto
    C<Apossible, you need to have a template conversion constructor,
    just like the standard smart-pointers do (std and boost):


    template<class Tstruct C
    {
    template<class U>
    C(C<Uconst&);
    };

    --
    Max

    Comment

    • James Kanze

      #3
      Re: Templates and inheritance

      On Oct 16, 4:35 pm, Isaac Gelado <igel...@gmail. comwrote:
      I am having problems with inheritance in templates classes.
      Say I have the following classes:
      class A {};
      class B: public A {};
      template<typena me Tclass C {};
      Now in my code I have something like:
      C<A **myAPtr;
      myAPtr = new C<B *>();
      I'm getting an error saying that conversion from C<B *to C<A *is
      not possible.
      Normal. C<B*and C<A*are two completely unrelated classes.
      Does it mean I can no use subclassing with templates without
      using ugly casts?
      Sure you can, but you're not deriving anything in the templates
      here. Derivation in template classes works exactly like
      derivation for any other class, and given:

      class Toto
      {
      // ...
      A* pA ;
      } ;

      class Titi
      {
      // ...
      B* pB ;
      } ;

      there's no implicit conversion (nor should there be) Titi* to
      Toto*. If you replace Toto with C<A*and Titi with C<B*>, why
      would you expect anything different?

      --
      James Kanze (GABI Software) email:james.kan ze@gmail.com
      Conseils en informatique orientée objet/
      Beratung in objektorientier ter Datenverarbeitu ng
      9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

      Comment

      Working...