D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 16060 - extern(C++) abstract base class and interface
Summary: extern(C++) abstract base class and interface
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Windows
: P1 major
Assignee: No Owner
URL:
Keywords: industry
Depends on:
Blocks:
 
Reported: 2016-05-23 01:16 UTC by Manu
Modified: 2021-03-24 13:03 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Manu 2016-05-23 01:16:43 UTC
In this case, we have an example of single-inheritance in C++, but D treats this construct as multiple-inheritance, and the struct's don't match.


C++:
----

#include <new>

class I
{
public:
  virtual void f1() = 0;
  virtual float f2() = 0;
};

class Test : public I
{
public:
  int t;

  Test(int t) : t(t) {}
};

class Blah : public Test
{
public:
  Blah() : Test(0xbaadf00d) {}

  void f1() {}
  float f2() { return 10.f; }
};

extern "C" {
  I* test()
  {
    return new Blah;
  }

  Test* test2()
  {
    return new Blah;
  }
}



D:
--

import std.stdio;

extern (C++) interface I
{
  void f1();
  float f2();
}

extern (C++) abstract class Test : I
{
  int t;
}

extern (C) I test();
extern (C) Test test2();

int main(string[] argv)
{
  auto i = test();
  i.f1();

  auto j = test2();
  float f = j.f2(); // CRASH! looks up vtable address: [i + 8], expect: vtable at [i]

  writeln("Hello D-World! ", f);
  return 0;
}
Comment 1 Nicholas Wilson 2021-03-24 06:31:08 UTC
This is probably the same issue as https://issues.dlang.org/show_bug.cgi?id=19192
Comment 2 Nicholas Wilson 2021-03-24 13:03:13 UTC
pretty sure this is fixed by https://github.com/dlang/dmd/pull/12302