如何重载条件运算符? [英] How to overload the conditional operator?

查看:141
本文介绍了如何重载条件运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能的重复:

操作员重载


我不知道如何在cpp中重载条件运算符?

  int a,b,c; 

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

是否有可能?

方案

几个操作符不能重载。这些运算符将名称而不是对象作为其右操作数:




  • 直接成员访问


  • 范围解析(::)



  • 此外,新的typecast操作符:static_cast<>,dynamic_cast<>,reinterpret_cast< ,and const_cast<>,并且#和##预处理器令牌不能重载。



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


    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?

    解决方案

    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

    这篇关于如何重载条件运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆