How to overload the conditional operator? [duplicate]

…衆ロ難τιáo~ 提交于 2019-11-26 17:23:44

问题


Possible Duplicate:
Operator overloading

I was wonder how can I over load the Conditional operator in cpp?

int a,b,c;

  a=10;
  b=11;
  c = (a>b) ? a : b;

Is it possible?


回答1:


Several operators cannot be overloaded. These operators take a name, rather than an object, as their right operand:

  • Direct member access (.)

  • Deference pointer to class member (.*)

  • Scope resolution (::)

  • Size of (sizeof)

The conditional operator (?:) also cannot be overloaded.

Additionally, the new typecast operators: static_cast<>, dynamic_cast<>, reinterpret_cast<>, and const_cast<>, and the # and ## preprocessor tokens cannot be overloaded.

http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=23




回答2:


You cannot overload the conditional operator.




回答3:


No, you can't overload the conditional operator, since it's simply shorthand for a simple if..else block.

You can however overload the operators used in the condition, but not for primitive types such as int, like you have in your example above.



来源:https://stackoverflow.com/questions/9428577/how-to-overload-the-conditional-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!