Fun with extern(C++)

Elie Morisse via Digitalmars-d digitalmars-d at puremagic.com
Tue Jan 26 13:07:41 PST 2016


On Tuesday, 26 January 2016 at 15:53:39 UTC, Manu wrote:
> So, you're saying that if you have an 'I' pointer, which may be 
> implemented by a C++ class, it still needs to be able to cast 
> to Object, and therefore fails? I wonder if a C++ class could 
> somehow be promoted to a D class when being derived in D, by 
> having an Object placed somewhere.

It helps that Object is a just a handful of virtual methods.

On Tuesday, 26 January 2016 at 16:25:35 UTC, Benjamin Thaut wrote:
> For a D class the first entry in the vtable is the classinfo. 
> Thus the problem if you derive a D class from a extern(C++) 
> base class. I don't see any way to actually fix this, adjusting 
> the this pointer won't help. Once you derive a D class from a 
> extern(C++) base class it is no longer a fully functional D 
> class. For example monitor (e.g. synchronized methods) won't 
> work.

Calypso has "hybrid" D classes inheriting from C++, with this 
layout:

  - D vptr
  - D monitor
   { start of C++ class }
  - C++ vptr
  - C++ fields...
  - might be other vptr and fields if the C++ class has more than 
one base
   { end of C++ class }
  - D fields...

and when downcasted to the C++ base class the "this" pointer gets 
adjusted to point towards the start of the C++ class.


More information about the Digitalmars-d mailing list