Is it perfectly legal to declare a pure virtual function twice (in two classes in an hierarchy)
Asked Answered
P

3

8

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() {}
};
Prince answered 22/11, 2013 at 12:31 Comment(11)
@Simple - some quote from the standard or something?Prince
@Kiril Kirov if he had, it were aAnswer and not a comment :PTarsometatarsus
I don't see why this deserves a down-vote, but..Prince
I don't see it as well, In my view a good question.Tarsometatarsus
I'm probably wrong, but I believe that omitting the declaration will provide the same behavior.Regulator
@KirilKirov there's nothing special going on. In B you are overriding A::f but declaring it pure virtual, so B is still abstract. Theres a small difference in that calling B::f() from C::f would actually call B::f whereas if you didn't override it in B it would call A::f.Stadium
@Stadium - thanks, I think exactly the same, but I wasn't sure about this. I still hope for some kind of proof, but I doubt there's anything about this in the standard, unfortunately.Prince
@KirilKirov: I think there is no need to address this specific case because is captured by the general case.Staple
Considering how virtual methods are (usually) implemented, this should not cause any problem: the virtual method table of B will just contain a null pointer for f 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
@remyabel - by the way, I didn't down-vote your answer, although it didn't help me.Prince
@LucTouraille - thanks for the shared thoughts, I agree with you.Prince
S
2

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.

Staple answered 22/11, 2013 at 12:35 Comment(2)
I don't really get your point. It's pure virtual, without implementation. And it's overriding, not overloading, which is significant here.Prince
Yes, I meant overriding. Sorry. But, maybe I missed your point... :-/. For me, there is no possible confusion based on functions respective types. So, they are not "the same".Staple
D
2

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().

Doggett answered 22/11, 2013 at 12:45 Comment(0)
A
1

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.

Assort answered 22/11, 2013 at 12:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.