D can not access C++ class static methods?

evilrat evilrat666 at gmail.com
Tue Jul 14 11:14:31 UTC 2020


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
>
> # sh build.sh
> 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.


More information about the Digitalmars-d mailing list