The question's title is pretty clear. Here's what I mean by example:
class A
{
public:
virtual void f() = 0;
};
class B: public A
{
public:
virtual void f() = 0;
};
class C: public B
{
public:
virtual void f() {}
};
The question's title is pretty clear. Here's what I mean by example:
class A
{
public:
virtual void f() = 0;
};
class B: public A
{
public:
virtual void f() = 0;
};
class C: public B
{
public:
virtual void f() {}
};
Yes this is legal because there are not the same functions at all. The B::f()
function is an overriding of A::f()
. The fact that f()
is virtual in both cases is not entering into account.
Yes, it is perfectly legal.
In most situations the declaration on f() in B doesn't change the meaning of the program in any way, but there's nothing wrong with a little redundancy.
Recall that the "= 0" means only that the class may not be instantiated directly; that all pure virtual function must be overridden before an object can be instantiated.
You can even provide a definition for a pure virtual function, which can be called on an instance of a subclass. By explicitly declaring B::f(), you leave open the option of giving a definition for B::f().
These three f() functions are difference but it is legal to declare same virtual function in two class because f() in A is overridden in F() in B. It function call depends on the class object.
so as according to above code , you do not have permission to create an instance of class A and B. hence every time the function define inside class C will be called.
© 2022 - 2024 — McMap. All rights reserved.
B
you are overridingA::f
but declaring it pure virtual, soB
is still abstract. Theres a small difference in that callingB::f()
fromC::f
would actually callB::f
whereas if you didn't override it inB
it would callA::f
. – StadiumB
will just contain a null pointer forf
instead of containing... a null pointer (well, probably not a null pointer, but you get my point). Of course, this is not a definitive answer since it assumes a particular implementation, but I don't really see why the standard would disallow such a thing. This is just an opinion though, I don't have time to dig through the standard to find the relevant quotes. – Jahn