How to call a extern C++ class constructor ?

Luhrel lucien.perregaux at gmail.com
Sat Feb 1 08:15:20 UTC 2020


Hello there,

I would like to know how can I call a C++ ctor.

Actually, I have this:

C++:
CppClass.cpp
----------------
#include "CppClass.h"


AmazingCppClass::AmazingCppClass()
{
	number = 124;
}

int AmazingCppClass::getNumber(bool show)
{
	if (show)
		printf("Number: %s", number);
	return number;
}

void AmazingCppClass::add(int num)
{
	number += num;
}
----------------
CppClass.h:
----------------
#include <stdio.h>


class AmazingCppClass
{
private:
	int number;

public:
	AmazingCppClass();
	int getNumber(bool show);
	void add(int num);
};
----------------

D:
app.d
----------------
import std.stdio;

void main()
{
	auto dcpp = new AmazingCppClass();
	dcpp.getNumber(true); //segfault here
}

extern(C++) class AmazingCppClass
{
	this();

	int getNumber(bool show);

	void add(int num);
}
----------------

But somehow I got a segfault on dcpp.getNumber(true).
I found that there's a __cpp_new 
(https://dlang.org/phobos/core_stdcpp_new_.html), but I have no 
idea how to use it and the doc doesn't say a lot about this 
(https://dlang.org/spec/cpp_interface.html#using_cpp_classes_from_d)

Do you guys know ?


More information about the Digitalmars-d-learn mailing list