Assignment to templated derived classes

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

    Assignment to templated derived classes

    This is partially an academic question, but I'm trying to understand
    templates better. I have a base class that uses template parameters to
    define the behavior of its class. I want to subclass this to extend
    the behavior. I also want to contain pointers to derived classes
    somehow, in a vector, say.

    My question is: How do I assign derived classes to the base class?

    Here is an example:

    template< int INT_TO_HANDLE >
    class Base
    {
    static _HandlesInt( int i )
    {
    return i == INT_TO_HANDLE;
    }
    };

    class DerivedHandlesO ne : public Base< 1 >
    {};

    class DerivedHandlesT wo : public Base< 2 >
    {};


    int main()
    {
    DerivedHandlesO ne one;
    Base *b = &one; // error: no template params
    Base<0> *b0 = &one; // error: cannot conver
    Base<1> *b0 = &one; // works! great, but how do I contain
    'DerivedHandles Two'?
    }

    I can add a super class to base:

    class SuperBase
    {
    };

    template< int FOO >
    class Base : public SuperBase
    {
    static _HandlesInt( int i )
    {
    return i == FOO;
    }
    };

    class DerivedHandlesO ne : public Base< 1 >
    {};

    class DerivedHandlesT wo : public Base< 2 >
    {};

    int main()
    {
    DerivedHandlesO ne one;
    DerivedHandlesT wo two;
    SuperBase *b1 = &one; // works
    SuperBase *b2 = &two; // works
    }


    It turns out that this solves my problem, which is that I have a
    'listener' implementation where I need specific handling for each
    event, but my question is:
    .. Why do I need the superclass for a pointer to the template? Without
    any types being visible in the interface isn't it the same,
    regardless?

    Thanks in advance,
    Aaron
  • David White

    #2
    Re: Assignment to templated derived classes

    "Art" <abrady@prontom ail.com> wrote in message
    news:6c283161.0 310180943.6a99a 513@posting.goo gle.com...[color=blue]
    > This is partially an academic question, but I'm trying to understand
    > templates better. I have a base class that uses template parameters to
    > define the behavior of its class. I want to subclass this to extend
    > the behavior. I also want to contain pointers to derived classes
    > somehow, in a vector, say.
    >
    > My question is: How do I assign derived classes to the base class?
    >
    > Here is an example:
    >
    > template< int INT_TO_HANDLE >
    > class Base
    > {
    > static _HandlesInt( int i )
    > {
    > return i == INT_TO_HANDLE;
    > }
    > };
    >
    > class DerivedHandlesO ne : public Base< 1 >
    > {};
    >
    > class DerivedHandlesT wo : public Base< 2 >
    > {};
    >
    >
    > int main()
    > {
    > DerivedHandlesO ne one;
    > Base *b = &one; // error: no template params
    > Base<0> *b0 = &one; // error: cannot conver
    > Base<1> *b0 = &one; // works! great, but how do I contain
    > 'DerivedHandles Two'?
    > }
    >
    > I can add a super class to base:
    >
    > class SuperBase
    > {
    > };
    >
    > template< int FOO >
    > class Base : public SuperBase
    > {
    > static _HandlesInt( int i )
    > {
    > return i == FOO;
    > }
    > };
    >
    > class DerivedHandlesO ne : public Base< 1 >
    > {};
    >
    > class DerivedHandlesT wo : public Base< 2 >
    > {};
    >
    > int main()
    > {
    > DerivedHandlesO ne one;
    > DerivedHandlesT wo two;
    > SuperBase *b1 = &one; // works
    > SuperBase *b2 = &two; // works
    > }
    >
    >
    > It turns out that this solves my problem, which is that I have a
    > 'listener' implementation where I need specific handling for each
    > event, but my question is:
    > . Why do I need the superclass for a pointer to the template? Without
    > any types being visible in the interface isn't it the same,
    > regardless?[/color]

    The class template creates a different, unrelated type for each different
    parameter you pass to it. The types Base<1> and Base<2> are as different as
    Ellipse and File. They are the base classes of separate class hierarchies.
    If you were able to declare a pointer as simply a Base*, what would you
    expect to happen if you called its _HandlesInt member?

    DW



    Comment

    Working...