Not familiar with D, will I have to assume that your first code fragment ends with an opIndexAssign call using value = someValue and name = someName ?
If so, it can be done in C ++, but not so simple. You can overload the [] operator and return the proxy object using the special = operator as follows (a very simple, far-fetched example):
class MyProxy { public: MyProxy (int& ref) : valueRef(ref) { } MyProxy& operator = (int value) { valueRef = value; return *this; } private: int& valueRef; }; class MyClass { public: MyProxy operator [] (std::string name); private: int myVal; }; MyProxy& MyClass::operator [] (std::string name) { if (name.compare("myVal")) return MyProxy(myVal); ... } int main ( ) { MyClass mc; mc["myVal"] = 10;
I would like to emphasize that the above is not a very beautiful / well-formed code, just an illustration. It has not been tested.
EDIT: Too fast for me Jeremiah !!!
source share