www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Deriving a D-class from a CPP-class

reply Alain De Vos <devosalain ymail.com> writes:
Is it ok to do the following ?
I.e. derive  a D-class from a CPP-class.
The compiler did not complained, and it was OK for him.
```
c++ -c MyClass.cpp
ldc2 main.d MyClass.o -L-lstdc++
```

```
extern(C++){
	class MyClass {
		public:
			int num=1;
			void myMethod();
	};
};

class Derived : MyClass{
};

int main(){
	Derived myObj;
	myObj.myMethod();
	return 0;
}

```

Could I use this method for using the C++-WXWIDGETS library 
directly?
Apr 28 2021
parent reply Alain De Vos <devosalain ymail.com> writes:
PS: I managed to link to phobo's/runtime & wxgtk at the same time.
It goes like this [So someone does not need to re-invent the weel]

```
ldc2 - c test.d
c++ test.o -o test -L/usr/local/lib -lphobos2-ldc -ldruntime-ldc 
-Wl,--gc-sections -lexecinfo -lpthread -lm -m64 
`wxgtk3u-3.0-config --cxxflags --libs`

```
Apr 28 2021
parent reply Alain De Vos <devosalain ymail.com> writes:
Following code produces a linker error.
d: error: undefined symbol: wxApp::OnInit()
```
extern(C++)
	{class wxApp {
			public:
			bool OnInit();
			//virtual bool Oninit();
		}
	}

class MYAPP: wxApp {
			alias OnInit=wxApp.OnInit;
			bool OnInit(){return true;};
}

int main(){
	return 0;
}
```
It is rather clear what I want to achieve but virtual functions 
give me headache because dlang does not now the word virtual.
Apr 28 2021
next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 28 April 2021 at 19:46:00 UTC, Alain De Vos wrote:
 Following code produces a linker error.
 d: error: undefined symbol: wxApp::OnInit()
 ```
 extern(C++)
 	{class wxApp {
 			public:
 			bool OnInit();
 			//virtual bool Oninit();
you mean `abstract` for that one?
 			alias OnInit=wxApp.OnInit;
idk what you intend to do with this, this pattern is for merging overloads not overriding
 			bool OnInit(){return true;};
and you might want `override` there
Apr 28 2021
prev sibling parent reply evilrat <evilrat666 gmail.com> writes:
On Wednesday, 28 April 2021 at 19:46:00 UTC, Alain De Vos wrote:
 It is rather clear what I want to achieve but virtual functions 
 give me headache because dlang does not now the word virtual.
It's virtual by default. The opposite is `final`.
Apr 28 2021
parent user1234 <user1234 12.de> writes:
On Thursday, 29 April 2021 at 06:38:53 UTC, evilrat wrote:
 On Wednesday, 28 April 2021 at 19:46:00 UTC, Alain De Vos wrote:
 It is rather clear what I want to achieve but virtual 
 functions give me headache because dlang does not now the word 
 virtual.
It's virtual by default. The opposite is `final`.
by default, excepted if protection is private or package.
Apr 29 2021