Is C++ virtual definition recursive? Consider
class Foo
{
public:
virtual void a()=0;
};
class Bar:public Foo
{
public:
void a()
{
//...
}
};
If I now inherit Bar and overload a again, is that a also polymorphic?
Recursive means that
Given a class
Athat has a virtual membera, and a virtual member of then:th subclass ofA, thenais also a virtual member of then+1:th subclass, for alln.
That is, virtual functions follow Peanos induction axiom and is not terminated after one level.
If you inherit from Bar you should have
class Bar:public Foo
{
public:
virtual void a() override
{
//...
}
};
So you are saying two things about a() here:
Bar will treat the function as virtuala from the base class Foo
As @MikeSeymour and @Bathsheba mentioned, the virtual keyword in Bar is superfluous as the function will be treated as virtual since it was in the base class. However, I tend to be in the habit of using virtual/override as shown in my example so it is immediately clear how this function is to be used at first glance of the class without having to walk up the inheritance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With