Function implemented outside the class

Rikki Cattermole alphaglosined at gmail.com
Mon Mar 24 22:55:22 PDT 2014


On Sunday, 23 March 2014 at 21:48:33 UTC, MarisaLovesUsAll wrote:
> Hi! I didn't find how to implement functions outside the class, 
> like in C++.
>
> C++:
> class MyClass
> {
>    void myFunc();
> };
> void MyClass::myFunc() { ... }
>
> Is it possible to do this in D ?
>
>
> Sorry for my bad English.
> Regards,
> Alexey

There is one way that could be made far nicer with templates and 
traits but..

import std.stdio;

alias void delegate() testfunc;

class T {
	this() {
		test = () { test_(this); };	
	}
	
	
	testfunc test;
}

void test_(T t) {
	writeln("hi from test");
}

void main() {
	T t = new T();
	t.test();
}

There will be other ways to make this nicer. If you feel you 
really really want this sort of thing (advised not to).


More information about the Digitalmars-d-learn mailing list