template and dynamic polymorphism

Discussion in 'C++' started by Jun, Aug 11, 2008.

  1. Jun

    Jun Guest

    struct Abstract{

    virtual void run() const = 0;

    };

    template <typename T>
    struct S : F{
    void run(){}
    };


    I just want to use as :

    Abstract* a = new S<policy_1>(); // doesn't work


    I just wanna to know how i could mix the templates with the
    traditional abstract class design, thank you in advance.
    Jun, Aug 11, 2008
    #1
    1. Advertising

  2. Jun

    Jun Guest

    Sorry, the code is :


    > struct Abstract{
    >
    >     virtual void run() const = 0;
    >
    > };
    >
    > template <typename T>
    > struct S : Abstract{
    >     void  run(){}
    >
    > };
    >
    > I just want to use as :
    >
    > Abstract* a = new S<policy_1>(); // doesn't work
    >
    > I just wanna to know how i could mix the templates with the
    > traditional abstract class design, thank you in advance.
    Jun, Aug 11, 2008
    #2
    1. Advertising

  3. Jun

    Jun Guest

    On Aug 11, 9:46 pm, "Alf P. Steinbach" <> wrote:
    > * Jun:
    >
    >
    >
    > > struct Abstract{

    >
    > >     virtual void run() const = 0;

    >
    > > };

    >
    > > template <typename T>
    > > struct S : F{
    > >     void  run(){}
    > > };

    >
    > > I just want to use as :

    >
    > > Abstract* a = new S<policy_1>(); // doesn't work

    >
    > > I just wanna to know how i could mix the templates with the
    > > traditional abstract class design, thank you in advance.

    >
    > How about deriving S from Abstract.
    >
    > Cheers, & hth.,
    >
    > - Alf
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?


    Sorry, my code is actually :

    struct S : Abstract // and still doesn't work
    Jun, Aug 11, 2008
    #3
  4. Jun

    Leandro Melo Guest

    On 11 ago, 17:09, Jun <> wrote:
    > Sorry, the code is :
    >
    > > struct Abstract{

    >
    > >     virtual void run() const = 0;

    >
    > > };

    >
    > > template <typename T>
    > > struct S : Abstract{
    > >     void  run(){}

    >
    > > };

    >
    > > I just want to use as :

    >
    > > Abstract* a = new S<policy_1>(); // doesn't work

    >
    > > I just wanna to know how i could mix the templates with the
    > > traditional abstract class design, thank you in advance.

    >
    >


    You get the error because you're hidding run() from class abstract (in
    S you're not making the function const). Try this:
    template <typename T>
    struct S : Abstract
    {
    void run()const {} //Notice the const at the end.
    };
    Leandro Melo, Aug 11, 2008
    #4
  5. Jun

    Rolf Magnus Guest

    Alf P. Steinbach wrote:

    > * Jun:
    >> struct Abstract{
    >>
    >> virtual void run() const = 0;
    >>
    >> };
    >>
    >> template <typename T>
    >> struct S : F{
    >> void run(){}
    >> };
    >>
    >>
    >> I just want to use as :
    >>
    >> Abstract* a = new S<policy_1>(); // doesn't work
    >>
    >>
    >> I just wanna to know how i could mix the templates with the
    >> traditional abstract class design, thank you in advance.

    >
    > How about deriving S from Abstract.


    Another thing that comes to mind would be to make run() in S const, like in
    Abstract, as well as telling, what "doesn't work" exactly means in this
    case.
    Rolf Magnus, Aug 11, 2008
    #5
  6. Jun

    Jun Guest

    On Aug 11, 10:14 pm, Rolf Magnus <> wrote:
    > Alf P. Steinbach wrote:
    > > * Jun:
    > >> struct Abstract{

    >
    > >>     virtual void run() const = 0;

    >
    > >> };

    >
    > >> template <typename T>
    > >> struct S : F{
    > >>     void  run(){}
    > >> };

    >
    > >> I just want to use as :

    >
    > >> Abstract* a = new S<policy_1>(); // doesn't work

    >
    > >> I just wanna to know how i could mix the templates with the
    > >> traditional abstract class design, thank you in advance.

    >
    > > How about deriving S from Abstract.

    >
    > Another thing that comes to mind would be to make run() in S const, like in
    > Abstract, as well as telling, what "doesn't work" exactly means in this
    > case.


    Thanks a lot, after changing the run as const in S, it works fine.
    Jun, Aug 11, 2008
    #6
  7. Jun

    Noah Roberts Guest

    Jun wrote:
    > On Aug 11, 10:14 pm, Rolf Magnus <> wrote:
    >> Alf P. Steinbach wrote:
    >>> * Jun:
    >>>> struct Abstract{
    >>>> virtual void run() const = 0;
    >>>> };
    >>>> template <typename T>
    >>>> struct S : F{
    >>>> void run(){}
    >>>> };
    >>>> I just want to use as :
    >>>> Abstract* a = new S<policy_1>(); // doesn't work
    >>>> I just wanna to know how i could mix the templates with the
    >>>> traditional abstract class design, thank you in advance.
    >>> How about deriving S from Abstract.

    >> Another thing that comes to mind would be to make run() in S const, like in
    >> Abstract, as well as telling, what "doesn't work" exactly means in this
    >> case.

    >
    > Thanks a lot, after changing the run as const in S, it works fine.
    >


    This will repeatedly mess with you. Make it the first thing you check.
    Noah Roberts, Aug 12, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Leslaw Bieniasz
    Replies:
    4
    Views:
    2,270
    Leslaw Bieniasz
    Oct 21, 2004
  2. Krivenok Dmitry
    Replies:
    13
    Views:
    1,385
    Axter
    Jun 1, 2006
  3. Replies:
    1
    Views:
    619
    Joshua Cranmer
    Jun 6, 2007
  4. TimeHorse
    Replies:
    0
    Views:
    302
    TimeHorse
    Aug 9, 2007
  5. none
    Replies:
    4
    Views:
    1,242
    Michael Doubez
    Mar 8, 2010
Loading...

Share This Page