Member functions C to D

Craig Kuhnert ckuhnert at
Wed Oct 7 01:29:22 PDT 2009

I am trying to convert some code I wrote in C++ to D to give it a try and I have come across some code that I dont know how to convert.
I have simplified the code to illustrate the problem I have.
How do I do this in D?

class IFieldSetter
	virtual void SetValue(void * object, const void * value) = 0;

template <class C, class T>
class FieldSetter : public IFieldSetter
	typedef T (C::* MemberField);
	 MemberField field;

	FieldSetter(MemberField afield)
		: field(afield)

	void SetTypedValue(C * object, const T& value)
		object->*field = value;
	void SetValue(void * object, const void * value)
		SetTypedValue((C*) object, (const T&) value);

class MySampleClass
	int Status;
	std::string Name;

void main(void)
	IFieldSetter * StatusSetter = new FieldSetter<MySampleClass,int>(&MySampleClass::Status);
	IFieldSetter * NameSetter   = new FieldSetter<MySampleClass,std::string>(&MySampleClass::Name);

	MySampleClass * a = new MySampleClass();
	MySampleClass * b = new MySampleClass();

	StatusSetter->SetValue(a, (void*)20);
	StatusSetter->SetValue(b, (void*)40);

	NameSetter->SetValue(a, "2002");
	NameSetter->SetValue(b, "2002");


More information about the Digitalmars-d-learn mailing list