Daniel
Daniel

Reputation: 19

C++ inheritance question

I have a class like so:

class A
{
   public:
      virtual void foo() { bar() }

   protected:  
      virtual void bar() { /* do stuff */ }
}

Now I want a derived class B that overrides both foo and bar. So I wrote the following:

class B : public A 
{
    public: 
        virtual void foo() { A::foo(); /* then other stuff */ }

    protected:
       virtual void bar() { /* do different stuff */ }
}

Everything compiles but when I invoke B::foo I expect B::bar to get (eventually) called. Instead, I get A::bar. What am I doing wrong?

Upvotes: 1

Views: 221

Answers (3)

Jess
Jess

Reputation: 3049

Other than the missing semicolons at the end of the class definitions, the current OP code works as intended.

Upvotes: 0

Tom
Tom

Reputation: 45104

before the update by the op.

This should work

A* b = new B();

b->bar(); //Notice that this is just an example

Also works with references

void B::foo(){this->bar();}

  B b;
  A& ab = b;
  ab.foo(); //calls B::bar()

Upvotes: 2

Nordic Mainframe
Nordic Mainframe

Reputation: 28737

Everything compiles but when I invoke B::foo I expect B::bar to get (eventually) called. Instead, I get A::bar. What am I doing wrong?

Looks like you didn't really understand what went wrong in your original code, decided that the virtual override mechanism must be the culprit and then you posted a non-working example which describes what you are inclined to believe, but which you didn't bother to check, because if you had then you'd seen that it does not expose the described behavior. here's a compileable version of your example.

#include <stdio.h>
class A
{
   public:
      virtual void foo() { puts("A:foo()"); bar(); }

   protected:  
      virtual void bar() { puts("A:bar()"); }
};

class B : public A 
{
    public: 
        virtual void foo() { puts("B:foo()"); A::foo(); }

    protected:
       virtual void bar() {  puts("B:bar()");  }
};


int main()
{
    B b;
    b.foo();
}

When I run this I get:

$ g++ g++ h.cc
$ ./a.out
B:foo()
A:foo()
B:bar()

So everything's fine with B::bar().

Upvotes: 9

Related Questions