Virtual functions

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

    Virtual functions

    Hi, I am trying to design a class, which calls all a specific virtual
    function from all it's subclasses. Like this:

    class Base
    {
    public:
    virtual void theFunction() { // stuff goes here };
    };

    class firstDerived : public Base
    {
    public:
    virtual void theFunction() { // stuff goes here };
    };

    class secondDerived : public firstDerived
    {
    public:
    virtual void theFunction() { // stuff goes here };
    };

    int main()
    {
    secondDerived* theClass = new secondDerived;
    ((Base*)secondD erived)->theFunction( );
    }

    Now I want to let the last line (((Base*)second Derived)->theFunction(); )
    call all of the theFunction()'s (the base's one, the first derived's one,
    and the second derived's one. (without pointers to the
    secondDerived:: theFunction())

    Is this possible?

    Leon
  • Victor Bazarov

    #2
    Re: Virtual functions

    Leon wrote:
    Hi, I am trying to design a class, which calls all a specific virtual
    function from all it's subclasses. Like this:
    >
    class Base
    {
    public:
    virtual void theFunction() { // stuff goes here };
    };
    >
    class firstDerived : public Base
    {
    public:
    virtual void theFunction() { // stuff goes here };
    };
    >
    class secondDerived : public firstDerived
    {
    public:
    virtual void theFunction() { // stuff goes here };
    };
    >
    int main()
    {
    secondDerived* theClass = new secondDerived;
    ((Base*)secondD erived)->theFunction( );
    }
    >
    Now I want to let the last line (((Base*)second Derived)->theFunction(); )
    call all of the theFunction()'s (the base's one, the first derived's one,
    and the second derived's one. (without pointers to the
    secondDerived:: theFunction())
    >
    Is this possible?
    Is this homework?

    First off, the end-of-line comment comments out your closing curly
    braces, so don't do

    { // stuff goes here };

    but do

    { /* stuff goes here */ };

    otherwise you look very sloppy. Second, get rid of the semicolons that
    follow the body of any function - those semicolons are either not needed
    or simply prohibited.

    Third, have you heard of calling the base class member function *from*
    the derived member function?

    class Foo {
    virtual void foo();
    };

    class Bar : public Foo {
    void foo() {
    Foo::foo(); // calls the base
    }
    };

    Fourth, in case you decide against this, you need to know that it's
    rather a bad design decision to make the base class aware of its derived
    classes.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask

    Comment

    • dhanu

      #3
      Re: Virtual functions

      On Aug 18, 10:39 pm, Leon <l...@the.netwr ote:
      Hi, I am trying to design a class, which calls all a specific virtual
      function from all it's subclasses. Like this:
      >
      class Base
      {
      public:
      virtual void theFunction() { // stuff goes here };
      >
      };
      >
      class firstDerived : public Base
      {
      public:
      virtual void theFunction() { // stuff goes here };
      >
      };
      >
      class secondDerived : public firstDerived
      {
      public:
      virtual void theFunction() { // stuff goes here };
      >
      };
      >
      int main()
      {
      secondDerived* theClass = new secondDerived;
      ((Base*)secondD erived)->theFunction( );
      >
      }
      >
      Now I want to let the last line (((Base*)second Derived)->theFunction(); )
      call all of the theFunction()'s (the base's one, the first derived's one,
      and the second derived's one. (without pointers to the
      secondDerived:: theFunction())
      >
      Is this possible?
      >
      Leon

      Could you please explain me what is the meaning of this statement
      secondDerived* theClass = new secondDerived;
      ((Base*)secondD erived)->theFunction( ); ----- ????

      I guess it would be ((Base*)theClas s)->theFunction( ); am i right??

      Comment

      • ihatespam

        #4
        Re: Virtual functions

        On Aug 18, 12:39 pm, Leon <l...@the.netwr ote:
        Hi, I am trying to design a class, which calls all a specific virtual
        function from all it's subclasses. Like this:
        >
        class Base
        {
                public:
                        virtual void theFunction() { // stuff goes here };
        >
        };
        >
        class firstDerived : public Base
        {
                public:
                        virtual void theFunction() { // stuff goes here };
        >
        };
        >
        class secondDerived : public firstDerived
        {
                public:
                        virtual void theFunction() { // stuff goes here };
        >
        };
        >
        int main()
        {
                secondDerived* theClass = new secondDerived;
                ((Base*)secondD erived)->theFunction( );
        >
        }
        >
        Now I want to let the last line (((Base*)second Derived)->theFunction(); )
        call all of the theFunction()'s (the base's one, the first derived's one,
        and the second derived's one. (without pointers to the
        secondDerived:: theFunction())
        >
        Is this possible?
        >
        Leon
        theClass->theFunction( );
        theClass->firstDerived:: theFunction();
        theClass->Base::theFunct ion();

        Comment

        Working...