C++ linkage specifics WRT virtual vs non-virtual member functions
digitalmars-d at puremagic.com
Sun Aug 13 00:42:15 PDT 2017
On Sunday, 13 August 2017 at 07:18:31 UTC, Roman Hargrave wrote:
> I'm writing some bindings and have run in to a dilemma due to
> the lack of explicit information on how the D compiler
> "decides" to use virtual dispatch for a member function or not.
> Specifically, I have a C++ library that _explicitly_ marks some
> member functions as virtual, in addition to a plethora of
> non-virtual functions.
> I would like to be certain that the D compiler understands that
> I would like it to treat those functions as virtual functions,
> but unlike C++ where there is a `virtual` classifier for
> functions, D does not appear to have such a facility.
> The C++ interop documentation suggests that both non- and
> virtual functions are supported, but further suggests that it
> only treats inherited functions as virtual with no method of
> explicitly requesting virtual dispatch for that method.
> As far as I can tell, all specified member functions in a class
> with C++ linkage are assumed to be non-virtual unless they are
W.r.t. virtual member functions, D's syntax is more similar to
Java than to C++, in that class member functions are virtual by
default, unless marked as `final`.
So to declare the following C++ class:
virtual int fun();
In D you have to do the following:
extern (C++) class Base
final int gun();
Like all other attributes, you can group multiple members to
which you want to apply the `final` attribute by either:
Though be aware that unlike the protection attributes (public,
private), there's no way to undo 'final:', so if you use it, you
would have to move it with all the non-virtual methods to the
bottom of the class definition.
More information about the Digitalmars-d