-1

Possible Duplicate:
How to force child same virtual function call its parent virtual function first

EDIT People are completely missing the point: What I was getting at is if alot of classes inherit Base, I don't want to have to call Base::myFunction() for every single one!


I'm not really sure how to word this question, but I hope it's apparent from the code (this may not actually compile, I wrote it quickly):

class Base
{
    bool flag = false;

    void myFunction ()
    {
        flag = true;

        // here run the inherited class's myFunction()
    }
};

class A : public Base
{
    void myFunction ()
    {
        // do some clever stuff without having to set the flags here.
    }
};

int main ()
{
    A myClass;
    myClass.myFunction(); // set the flags and then run the clever stuff
    std::cout << myClass.flag << endl; // should print true
    return 0;
}
Community
  • 1
  • 1
Cheetah
  • 13,785
  • 31
  • 106
  • 190

4 Answers4

4

First of all - use virtual functions for the case if you will use pointers. Then you just need to call base class myFunction in derived class realization. See example:

class Base
{
    bool flag = false;

    virtual void myFunction ()
    {
        flag = true;
    }
};

class A : public Base
{
    virtual void myFunction ()
    {
        Base::myFunction();  // call base class implementation first
        // do some clever stuff without having to set the flags here.
    }
};

int main ()
{
    A myClass;

    myClass.myFunction(); // set the flags and then run the clever stuff

    std::cout << myClass.flag << endl; // should print true

    return 0;
}

If you not likes to call your base class function in all derived classes. You can add special virtual function for "clever" calculations and realize it separately in all derived classes. Example:

class Base
{
    bool flag = false;

    virtual void cleverCalc() = 0;
    virtual void myFunction ()
    {
        flag = true;
        cleverCalc();
    }
};

class A : public Base
{
    virtual void cleverCalc()
    {
        // do some clever stuff without having to set the flags here.
    }
};
johngull
  • 819
  • 6
  • 22
  • I already knew this method and wanted to avoid it....but its the closest answer so I shall accept it. – Cheetah May 01 '12 at 20:20
3
class A : public Base
{
    void myFunction ()
    {
        Base::myFunction();    // <-------------------------------
        // do some clever stuff without having to set the flags here.
    }
};
David
  • 27,652
  • 18
  • 89
  • 138
  • What I was getting at is if alot of classes inherit Base, I don't want to have to call `Base::myFunction()` for every single one! – Cheetah May 01 '12 at 20:11
  • @Ben Then make your action part of the constructor. That's the only way to really automatically call something from a base class. – chrisaycock May 01 '12 at 20:12
  • @chrisaycock - Not possible to do what I want then...Thanks. – Cheetah May 01 '12 at 20:14
  • @Ben: then you're out of luck I'm afraid. Consider: 5 years from now, someone writes a new subclass of `Base`. How will `Base::myFunction()` know what code to run? – suszterpatt May 01 '12 at 20:14
2

You make another function with an empty implementation (which will be overriden in subclasses), and call that in your myFunction. Something like this:

class Base
{
    bool flag = false;

    void myFunction ()
    {
        flag = true;

        // here run the inherited class's myFunction()
        myDerivedFunction();
    }

    virtual void myDerivedFunction()
    {
        // this should be implemented by subclasses.
    }
};

class A : public Base
{
    void myDerivedFunction ()
    {
        // do some clever stuff without having to set the flags here.
    }
};
Daniel Gabriel
  • 3,939
  • 2
  • 26
  • 37
  • +1 to force subclasses to implement you could make `myDerivedFunction()` pure virtual. – hmjd May 01 '12 at 20:22
0

By virtue of your inheritance structure, the myFunction() call in your derived class precludes the call of the version in the base class, thus flag is never set to "true."

David W
  • 10,062
  • 34
  • 60