polymorphic use problem

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

    #1

    polymorphic use problem

    If I have these classes:


    class TBase
    {
    public:
    virtual int Execute(Server* srv) = 0;
    virtual ~TFunction() {}
    };

    class Derived : public TBase
    {
    public:
    Derived(const std::string& strA, int ia1)
    : m_strA(strA), m_ia1(ia1){
    }

    virtual int Execute(TServer srv)
    {
    return SomeFunction(sr v, m_strA, m_ia1);
    }
    ~TRegister() { }
    private:
    std::string m_strA;
    int m_ia1;
    };


    I want to call the Execute function on a Derived object. How can I
    call it?
  • Joe Greer

    #2
    Re: polymorphic use problem

    Angus <anguscomber@gm ail.comwrote in news:67a176cb-5052-45d3-9dc4-
    6bc47f3fff1f@27 g2000hsf.google groups.com:
    If I have these classes:
    >
    >
    class TBase
    {
    public:
    virtual int Execute(Server* srv) = 0;
    virtual ~TFunction() {}
    };
    >
    class Derived : public TBase
    {
    public:
    Derived(const std::string& strA, int ia1)
    : m_strA(strA), m_ia1(ia1){
    }
    >
    virtual int Execute(TServer srv)
    {
    return SomeFunction(sr v, m_strA, m_ia1);
    }
    ~TRegister() { }
    private:
    std::string m_strA;
    int m_ia1;
    };
    >
    >
    I want to call the Execute function on a Derived object. How can I
    call it?
    >

    Derived d("blah", 5);

    TServer s;
    d.Execute(s);

    joe

    Comment

    • Joe Greer

      #3
      Re: polymorphic use problem

      Angus <anguscomber@gm ail.comwrote in news:67a176cb-5052-45d3-9dc4-
      6bc47f3fff1f@27 g2000hsf.google groups.com:
      If I have these classes:
      >
      >
      class TBase
      {
      public:
      virtual int Execute(Server* srv) = 0;
      virtual ~TFunction() {}
      };
      >
      class Derived : public TBase
      {
      public:
      Derived(const std::string& strA, int ia1)
      : m_strA(strA), m_ia1(ia1){
      }
      >
      virtual int Execute(TServer srv)
      {
      return SomeFunction(sr v, m_strA, m_ia1);
      }
      ~TRegister() { }
      private:
      std::string m_strA;
      int m_ia1;
      };
      >
      >
      I want to call the Execute function on a Derived object. How can I
      call it?
      >
      I have to point out that as written this won't compile because int
      Execute(Server *) isn't implemented any where (unless TServer is a
      typedef for a Server *). However, assuming you meant them to be the
      same...

      If you had:

      void f(TBase & t)
      {
      Server s;
      t.Execute(&s);
      }
      ..
      ..
      ..

      int main()
      {
      Derived d("blah", 4);

      f(d);
      }


      Is that what you have in mind? Or was it more like:

      void f(TBase * t)
      {
      Server s;
      t->Execute(&s);
      }

      int main()
      {
      Derived d("blah", 3);
      TBase * pT = &d;

      f(pT);
      }

      Either way works. Polymorphism requires a pointer or reference at some
      point or you aren't really polymorphic.

      joe

      Comment

      • red floyd

        #4
        Re: polymorphic use problem

        On Jul 10, 9:55 am, Joe Greer <jgr...@doublet ake.comwrote:
        Angus <anguscom...@gm ail.comwrote in news:67a176cb-5052-45d3-9dc4-
        6bc47f3ff...@27 g2000hsf.google groups.com:
        >
        >
        >
        If I have these classes:
        >
        class TBase
        {
        public:
            virtual int Execute(Server* srv) = 0;
            virtual ~TFunction() {}
        };
        >
        class Derived : public TBase
        {
        public:
            Derived(const std::string& strA, int ia1)
            : m_strA(strA), m_ia1(ia1){
            }
        >
            virtual int Execute(TServer srv)
            {
                return SomeFunction(sr v, m_strA, m_ia1);
            }
            ~TRegister() { }
        private:
            std::string m_strA;
            int m_ia1;
        };
        >
        I want to call the Execute function on a Derived object.  How can I
        call it?
        >
        I have to point out that as written this won't compile because int
        Execute(Server *) isn't implemented any where (unless TServer is a
        typedef for a Server *).  However, assuming you meant them to be the
        same...
        >
        I also have to point out that these won't compile because the
        destructors are improperly named.

        Comment

        • Joe Greer

          #5
          Re: polymorphic use problem

          red floyd <redfloyd@gmail .comwrote in news:11d9e89e-6410-44ad-9675-
          db3db1f230aa@m3 6g2000hse.googl egroups.com:
          On Jul 10, 9:55 am, Joe Greer <jgr...@doublet ake.comwrote:
          >I have to point out that as written this won't compile because int
          >Execute(Serv er *) isn't implemented any where (unless TServer is a
          >typedef for a Server *).  However, assuming you meant them to be the
          >same...
          >>
          I also have to point out that these won't compile because the
          destructors are improperly named.
          >
          There is that too. :)

          joe

          Comment

          Working...