www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - D can not access C++ class static methods?

reply zoujiaqing <zoujiaqing gmail.com> writes:
/// Test.hpp

#include <iostream>

class Test
{
     static Test* Create();

     void test();
};


/// Test.cpp

#include "Test.hpp"

Test* Test::Create()
{
     return new Test();
}

void Test::test()
{
     std::cout << "test" << std::endl;
}



/// main.d

import std.stdio;

extern(C++)
{
     class Test
     {
          disable this();

         static Test Create();

         void test();
     }
}

void main()
{
     Test canvas = Test.Create();

     canvas.test();
}



/// build.sh

clang++ -I./include/ -fPIE -c src/Test.cpp
dmd source/main.d Test.o -L-lstdc++
./main



/// ERROR


build.sh: line 4: 59159 Segmentation fault: 11  ./main
Jul 14 2020
parent evilrat <evilrat666 gmail.com> writes:
On Tuesday, 14 July 2020 at 10:32:17 UTC, zoujiaqing wrote:
 /// Test.hpp

 #include <iostream>

 class Test
 {
     static Test* Create();

     void test();
 };


 /// Test.cpp

 #include "Test.hpp"

 Test* Test::Create()
 {
     return new Test();
 }

 void Test::test()
 {
     std::cout << "test" << std::endl;
 }



 /// main.d

 import std.stdio;

 extern(C++)
 {
     class Test
     {
          disable this();

         static Test Create();

         void test();
     }
 }

 void main()
 {
     Test canvas = Test.Create();

     canvas.test();
 }



 /// build.sh

 clang++ -I./include/ -fPIE -c src/Test.cpp
 dmd source/main.d Test.o -L-lstdc++
 ./main



 /// ERROR


 build.sh: line 4: 59159 Segmentation fault: 11  ./main
I already replied about this for your question in learn section. Put print in ctor call and you'll see it most likely does works. Or put a null check on D side, just whatever. Do proper debug, don't just guess. Most likely it isn't working because you have wrong translation. D classes have virtual by default methods, and your C++ method isn't virtual, so mark it 'final' on D side and this should fix the problem.
Jul 14 2020