سؤال

I want to overload array operator in this way.

A b;
b->c;

and b-> c should expand to boverloadarrayfunction("c"); Is it possible?

Edit: Disclaimer I know it is bad thing.

Why: I want to add level of indirection to QueryInterface.

هل كانت مفيدة؟

المحلول

  1. operator-> is not the array operator. None of the C++ operators is officially called that way, but the one that fits that name best would be the indexing opeator[].
  2. What you want is not possible. In b->c, however that might be implemented, c is a symbol, i.e. the name of some variable or function. "c" on the other hand is a string literal, and the first cannot be converted to the latter (except by some black preprocessor magic, which does not fit in the expression b->c).

You might want to look up how overloading operator-> works, because it is special in the sense that it has to return either a pointer or another object that has operator-> overloaded. The compiler will call the -> on any returned object until it truly dereferences a pointer.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top