What does C++ do better than D? - StackOverflow.com

Jonathan M Davis jmdavisProg at gmx.com
Sat Jul 30 13:50:35 PDT 2011


On Saturday 30 July 2011 10:16:49 bearophile wrote:
> Jonathan M Davis:
> 
> This is part 7 of your answer:
> >Member functions of classes are polymorphic by default. You can't declare
> >them non-virtual. It's up to the compiler to decide if they can be (which
> >really is only the case if they're final and aren't overriding a function
> >from a base class). So, that could be a performance problem in some
> >cases. However, if you really don't need the polymorphism, then all you
> >have to do is use structs, and it's not an issue.<
> 
> Do you want to declare a class member non-virtual but non-final too?

In cases where a function is not overriding another function and there isn't 
currently a class which derives from that class or there isn't currently a 
function in a derived class which overrides it, it would be nice if it weren't 
virtual. For a variety of reasons, that's not feasible, and I think that D is, 
overall, making the correct decision about virtuality. However, from the 
perspective of a C++ programmer just learning D, forcing virtual on every 
class member function except in cases where the compiler might optimize the 
virtuality out under the hood could definitely be viewed as a negative. struct 
pretty much solves the problem, since if you didn't really want polymorphism 
(which would generally by why you wouldn't declare a function virtual), it 
does what you want. But from the perspective of a C++ programmer wanting 
maximum control, the situation with virtual functions in D _could_ be viewed 
as a downside, hence why I mentioned it.

- Jonathan M Davis


More information about the Digitalmars-d mailing list