KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I am trying to find a way to use C++ classes in D. http://www.digitalmars.com/d/2.0/cpp_interface.html D cannot call C++ special member functions, and vice versa. These include constructors, destructors, conversion operators, operator overloading, and allocators. So, I am attempting to dumb down these C++ functions to C style function calls. Here is the proof I am working with. helper.h class someClass { public: someClass(); char *whatSayYou(); }; extern "C" { someClass *hearMeOut(); } helper.cpp #include "helper.h" someClass::someClass() { } char *someClass::whatSayYou() { return "Everything is gravy"; } someClass *hearMeOut() { return new someClass; } main.d import std.stdio; int main(string[] args) { someClass *awesomeExample = hearMeOut(); char *shoutoutToTheWorld = awesomeExample.whatSayYou(); writefln(std.string.toString(shoutoutToTheWorld)); return 0; } extern (C++) { interface someClass { char *whatSayYou(); } someClass *hearMeOut(); } And here is how I complied it. g++-4.3 -c -I code/dg3d_helper -I /usr/local/include/ -o code/dg3d_helper/helper.o code/dg3d_helper/helper.cpp code/dg3d_helper/helper.cpp: In member function ‘char* someClass::whatSayYou()’: code/dg3d_helper/helper.cpp:19: warning: deprecated conversion from string constant to ‘char*’ gdc-4.3 -g -c -I code/ -o code/main.o code/main.d gdc-4.3 -g -I code/ -o main code/dg3d_helper/helper.o code/main.o -lstdc++ And I get a segmentation fault as soon as the method is called. Program received signal SIGSEGV, Segmentation fault. 0x0000000000402fa0 in _Dmain (args=...) at code/main.d:7 7 char *shoutoutToTheWorld = aweso
Tags (comma-separated)
Save Edits
Cancel