Wballer3
Wballer3

Reputation: 151

VIrtual base function called in base class, C++

I have a class:

class B
{
public:
  FuncCallingFoo();

protected:
   virtual Foo (const arg argument);
}:

Now, function Foo is used within the body of class B, i.e. it gets called somewhere in the definition of FuncCallingFoo.

I also have class A, which inherits from B and has it's own implementation of Foo:

class A:B
{
...

protected:
   Foo (const arg argument);
};

Let's say I create an instance of class A:

A a;

Furthermore, assume I do the following call:

a.FuncCallingFoo();

will this result in the implementation of Foo from class A being called?

Upvotes: 0

Views: 56

Answers (1)

Ferenc Deak
Ferenc Deak

Reputation: 35448

No, because you have declared Foo as protected in class A and B and you have not requested for public inheritance: class A:B (Yes, this is still valid C++).

Actually your program won't compile (after fixing the default return values and argument errors) due to this issue.

See: Difference between private, public, and protected inheritance

Upvotes: 1

Related Questions