Virtual functions, call all of them, not only the last derived.

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

    Virtual functions, call all of them, not only the last derived.

    I have a question about virtual functions, and all that stuff.

    Let's say, I have three classes.

    class Base;
    class firstDerived;
    class secondDerived;

    I defined them as following:

    #include <iostream>
    using namespace std;

    class Base
    {
    public:
    virtual void theMagicalVeryU sefullFunction( );
    };

    void Base::theMagica lVeryUsefullFun ction()
    {
    cout << "Called from the base class" << endl;
    }

    class firstDerived
    {
    public:
    virtual void theMagicalVeryU sefullFunction( );
    };

    void firstDerived::t heMagicalVeryUs efullFunction()
    {
    cout << "Called from the first derived class" << endl;
    }

    class secondDerived
    {
    public:
    virtual void theMagicalVeryU sefullFunction( );
    };

    void secondDerived:: theMagicalVeryU sefullFunction( )
    {
    cout << "Called from the secondDerived class" << endl;
    }

    int main(int argc, char* argv[])
    {
    secondDerived* theObject = new secondDerived;
    ((Base*)theObje ct)->theMagicalVery UsefullFunction ();
    }

    This program doesn't exactly do what I want it to do, which is completely
    normal. What I want it to do, is call ALL of the
    theMagicalVeryU sefullFunction( )'s, including the one from the Base, the
    firstDerived and at last the secondDerived's one. Is that possible,
    without a ((secondDerived *)this)->theMagicalVery UsefullFunction () call in
    the base class (or so?)
  • Obnoxious User

    #2
    Re: Virtual functions, call all of them, not only the last derived.

    On Sun, 28 Sep 2008 14:15:50 +0000, The Doctor wrote:
    I have a question about virtual functions, and all that stuff.
    >
    Let's say, I have three classes.
    >
    class Base;
    class firstDerived;
    class secondDerived;
    >
    I defined them as following:
    >
    #include <iostream>
    using namespace std;
    >
    class Base
    {
    public:
    virtual void theMagicalVeryU sefullFunction( );
    };
    >
    void Base::theMagica lVeryUsefullFun ction() {
    cout << "Called from the base class" << endl;
    }
    >
    class firstDerived
    I assume: class firstDerived : public Base
    {
    public:
    virtual void theMagicalVeryU sefullFunction( );
    };
    >
    void firstDerived::t heMagicalVeryUs efullFunction() {
    cout << "Called from the first derived class" << endl;
    }
    >
    class secondDerived
    I assume again: class secondDerived : public Base
    {
    public:
    virtual void theMagicalVeryU sefullFunction( );
    };
    >
    void secondDerived:: theMagicalVeryU sefullFunction( ) {
    cout << "Called from the secondDerived class" << endl;
    }
    >
    int main(int argc, char* argv[])
    {
    secondDerived* theObject = new secondDerived;
    ((Base*)theObje ct)->theMagicalVery UsefullFunction ();
    }
    >
    This program doesn't exactly do what I want it to do, which is
    completely normal. What I want it to do, is call ALL of the
    theMagicalVeryU sefullFunction( )'s, including the one from the Base, the
    firstDerived and at last the secondDerived's one. Is that possible,
    without a ((secondDerived *)this)->theMagicalVery UsefullFunction () call
    in the base class (or so?)
    You mean like:

    void firstDerived::t heMagicalVeryUs efullFunction()
    {
    Base::theMagica lVeryUsefullFun ction();
    cout << "Called from the first derived class" << endl;
    }

    void secondDerived:: theMagicalVeryU sefullFunction( )
    {
    firstDerived::t heMagicalVeryUs efullFunction() ;
    cout << "Called from the secondDerived class" << endl;
    }

    --
    OU
    Remember 18th of June 2008, Democracy died that afternoon.

    Comment

    • Obnoxious User

      #3
      Re: Virtual functions, call all of them, not only the last derived.

      On Sun, 28 Sep 2008 10:15:50 -0500, Obnoxious User wrote:
      On Sun, 28 Sep 2008 14:15:50 +0000, The Doctor wrote:
      >
      >I have a question about virtual functions, and all that stuff.
      >>
      >Let's say, I have three classes.
      >>
      >class Base;
      >class firstDerived;
      >class secondDerived;
      >>
      >I defined them as following:
      >>
      >#include <iostream>
      >using namespace std;
      >>
      >class Base
      >{
      > public:
      > virtual void theMagicalVeryU sefullFunction( );
      >};
      >>
      >void Base::theMagica lVeryUsefullFun ction() {
      > cout << "Called from the base class" << endl;
      >}
      >>
      >class firstDerived
      >
      I assume: class firstDerived : public Base
      >
      >{
      > public:
      > virtual void theMagicalVeryU sefullFunction( );
      >};
      >>
      >void firstDerived::t heMagicalVeryUs efullFunction() {
      > cout << "Called from the first derived class" << endl;
      >}
      >>
      >class secondDerived
      >
      I assume again: class secondDerived : public Base
      >
      I meant: class secondDerived : public firstDerived

      --
      OU
      Remember 18th of June 2008, Democracy died that afternoon.

      Comment

      • gw7rib@aol.com

        #4
        Re: Virtual functions, call all of them, not only the last derived.

        On 28 Sep, 15:15, The Doctor <do....@email.m ewrote:
        What I want it to do, is call ALL of the
        theMagicalVeryU sefullFunction( )'s, including the one from the Base, the
        firstDerived and at last the secondDerived's one. Is that possible,
        without a ((secondDerived *)this)->theMagicalVery UsefullFunction () call in
        the base class (or so?)
        Other than what Obnoxious User has suggested, is there any way you
        could rig things so that theMagicalVeryU sefullFunction is in fact the
        constructor or the destructor? These get called all the way down.

        Without seeing your code, I can't tell whether this has the remotest
        chance of working, but it might (perhaps!) be a possibility.

        Comment

        • The Doctor

          #5
          Re: Virtual functions, call all of them, not only the last derived.

          On Sun, 28 Sep 2008 10:15:50 -0500, Obnoxious User wrote:
          On Sun, 28 Sep 2008 14:15:50 +0000, The Doctor wrote:
          >
          >I have a question about virtual functions, and all that stuff.
          >>
          >Let's say, I have three classes.
          >>
          >class Base;
          >class firstDerived;
          >class secondDerived;
          >>
          >I defined them as following:
          >>
          >#include <iostream>
          >using namespace std;
          >>
          >class Base
          >{
          > public:
          > virtual void theMagicalVeryU sefullFunction( );
          >};
          >>
          >void Base::theMagica lVeryUsefullFun ction() {
          > cout << "Called from the base class" << endl;
          >}
          >>
          >class firstDerived
          >
          I assume: class firstDerived : public Base
          >
          >{
          > public:
          > virtual void theMagicalVeryU sefullFunction( );
          >};
          >>
          >void firstDerived::t heMagicalVeryUs efullFunction() {
          > cout << "Called from the first derived class" << endl;
          >}
          >>
          >class secondDerived
          >
          I assume again: class secondDerived : public Base
          >
          >{
          > public:
          > virtual void theMagicalVeryU sefullFunction( );
          >};
          >>
          >void secondDerived:: theMagicalVeryU sefullFunction( ) {
          > cout << "Called from the secondDerived class" << endl;
          >}
          >>
          >int main(int argc, char* argv[])
          >{
          > secondDerived* theObject = new secondDerived;
          > ((Base*)theObje ct)->theMagicalVery UsefullFunction ();
          >}
          >>
          >This program doesn't exactly do what I want it to do, which is
          >completely normal. What I want it to do, is call ALL of the
          >theMagicalVery UsefullFunction ()'s, including the one from the Base, the
          >firstDerived and at last the secondDerived's one. Is that possible,
          >without a ((secondDerived *)this)->theMagicalVery UsefullFunction () call
          >in the base class (or so?)
          >
          You mean like:
          >
          void firstDerived::t heMagicalVeryUs efullFunction() {
          Base::theMagica lVeryUsefullFun ction(); cout << "Called from the
          first
          derived class" << endl;
          }
          >
          void secondDerived:: theMagicalVeryU sefullFunction( ) {
          firstDerived::t heMagicalVeryUs efullFunction() ; cout << "Called
          from the
          secondDerived class" << endl;
          }
          Thanks, that helped me out. By the way I forgot the public stuff ;)

          Comment

          Working...