Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
D (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Interaction with C++ code==== For D code marked as <code>extern(C++)</code>, the following features are specified: * The name mangling conventions shall match those of C++ on the target. * For function calls, the ABI shall be equivalent. * The vtable shall be matched up to single inheritance (the only level supported by the D language specification). C++ namespaces are used via the syntax <code>extern(C++, namespace)</code> where ''namespace'' is the name of the C++ namespace. =====An example of C++ interoperation===== '''The C++ side''' <syntaxhighlight lang="C++"> import std; class Base { public: virtual void print3i(int a, int b, int c) = 0; }; class Derived : public Base { public: int field; Derived(int field): field(field) {} void print3i(int a, int b, int c) { std::println("a = {}", a); std::println("b = {}", b); std::println("c = {}", c); } int mul(int factor); }; int Derived::mul(int factor) { return field * factor; } Derived* createInstance(int i) { return new Derived(i); } void deleteInstance(Derived*& d) { delete d; d = 0; } </syntaxhighlight> '''The D side''' <syntaxhighlight lang="D"> extern(C++) { abstract class Base { void print3i(int a, int b, int c); } class Derived : Base { int field; @disable this(); override void print3i(int a, int b, int c); final int mul(int factor); } Derived createInstance(int i); void deleteInstance(ref Derived d); } void main() { import std.stdio; auto d1 = createInstance(5); writeln(d1.field); writeln(d1.mul(4)); Base b1 = d1; b1.print3i(1, 2, 3); deleteInstance(d1); assert(d1 is null); auto d2 = createInstance(42); writeln(d2.field); deleteInstance(d2); assert(d2 is null); } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)