1

I am experiencing a problem where a derived class does not have it's own version of a function called when it is called from a base class pointer. To better explain the classes are defined as below

Class Foo
{
public:
  Foo();
  virtual ~Foo();

  virtual void Event();
}

//-----------------------

Class FooBar : public Foo
{
public:
  FooBar();

  virtual void Update() = 0;
  virtual void Draw() = 0;
}

//-----------------------

Class FinalFoo : public FooBar
{
public:
  FinalFoo();

  void Update();
  void Draw();

  void Event();
}

There are other classes similar to FinalFoo. So I attempt to call Event on a pointer to a Foo object expecting that it would call the derived implementation. However, it would appear that it calls the base class version and that is all

FinalFoo* myThing = new FinalFoo();
Foo* baseThing = myThing;

baseThing->Event();  // I expected this to call FinalFoo::Event()
5
  • 1
    You've failed at providing a testcase, because what you are saying makes no sense. Commented Sep 1, 2013 at 14:45
  • Your final snippet might as well be FinalFoo myThing; Foo &baseThing = myThing; Commented Sep 1, 2013 at 14:45
  • In the above setup it would, indeed, call FinalFoo::Event(). However, the example is clearly not the original code (it contains a few errors, e.g., missing semicolons after your class definitions) and there are subtle changes in the signature which may prevent the function you think is overriding the base class virtual function from doing so. If you have C++11 add override after each overriding function and the compiler will tell you if it is, indeed, an override! Commented Sep 1, 2013 at 14:45
  • It should definitely call the derived class implementation. There is something else going on which you haven't shown. Commented Sep 1, 2013 at 14:46
  • Did you at least test and make sure that Event does get called for the FinalFoo object, myThing? Also, what happens if you make Event in Foo a pure virtual by adding = 0? Commented Sep 1, 2013 at 14:46

1 Answer 1

2

Assuming the above code is corrected, it actually does call FinalFoo::Event(). below is a complete and compilable example. Note, that it also adds the keyword override in strategic points: I'd bet that adding override in the original code, too (and compiling with a compiler aware of this keyword) would point out that your override isn't one.

#include <iostream>

class Foo
{
public:
    virtual ~Foo() {}
    virtual void Event() { std::cout << "Foo::Event()\n"; }
};

//-----------------------

class FooBar : public Foo
{
public:
    virtual void Update() = 0;
};

//-----------------------

class FinalFoo : public FooBar
{
public:
    FinalFoo() {}

    void Update() override { std::cout << "FinalFoo::Update()\n"; }
    void Event() override  { std::cout << "FinalFoo::Event()\n"; }
};

int main()
{
    FinalFoo myThing;
    Foo* baseThing = &myThing;

    baseThing->Event();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Good. I tried your code in codepad.org and it worked. Previously I had thought there should be a declaration of Event also in FooBar, which clearly is not the case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.