1

I have a thirdparty C++ class I don't want to change.

I would like to write a wrapper which would behave like indexer operator without modification of the original class. I can do it using a helper class and proxy pattern, but is there something like extensions in C#. Something like friend indexer operator?

Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148

1 Answers1

1

If you can use public inheritance to overload operator[] then it is the easiest way to go.

Unfortunately operator[] cannot be non-member, so defining it as non-member function is not possible.

pstanisz
  • 181
  • 9
  • Public inheritance is not necessary at all. You can also inherit privately or use aggregation. Or, you could use a reference/pointer to the original object withou any ownership implications. – Ulrich Eckhardt Apr 21 '15 at 05:49
  • True - I've just written that it is probably the easiest way. – pstanisz Apr 21 '15 at 05:51
  • @qub1n also have in mind that unlike C# "indexer" operator in C++ can accept only one parameter to be properly invoked – W.F. Apr 21 '15 at 05:52
  • 1
    As for extension methods see the post of dennis from the thread: http://stackoverflow.com/questions/5463009/extension-methods-in-c it is for my knowledge the best way of imitating extension methods in c++ – W.F. Apr 21 '15 at 05:58
  • OK, "operator[] cannot be non-member" is fine answer to me. – Tomas Kubes Apr 21 '15 at 08:42