template and dynamic polymorphism

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

    #1

    template and dynamic polymorphism

    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

    #2
    Re: template and dynamic polymorphism

    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.

    Comment

    • Leandro Melo

      #3
      Re: template and dynamic polymorphism

      On 11 ago, 17:09, Jun <junh...@gmail. comwrote:
      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.
      };

      Comment

      • Rolf Magnus

        #4
        Re: template and dynamic polymorphism

        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.


        Comment

        • Jun

          #5
          Re: template and dynamic polymorphism

          On Aug 11, 10:14 pm, Rolf Magnus <ramag...@t-online.dewrote:
          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.

          Comment

          • Noah Roberts

            #6
            Re: template and dynamic polymorphism

            Jun wrote:
            On Aug 11, 10:14 pm, Rolf Magnus <ramag...@t-online.dewrote:
            >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
            >>>traditiona l 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.

            Comment

            Working...