zoukankan      html  css  js  c++  java
  • Item 26. Operator Function Lookup

    Item 26. Operator Function Lookup

    有时候Operator Function看起来像是成员操作符函数overload一个非成员操作符,但事实并非如此。这不是overloading,只是一种不同的查找算法。
    class X {
      public:
        X operator %( const X & ) const; // binary modulus
        X memFunc1( const X & );
        void memFunc2();
        //...
    };

    X a, b, c;
    a = b % c; // infix call to member operator %
    a = b.operator %( c ); // member function call
    a = b.memFunc1( c ); // another member function call

    当使用member function call时,查找规则遵循 Member Function Lookup,使用infix call却有所不同,eg:
    X operator %( const X &, int ); // non-member operator
    //...
    void X::memFunc2() {
         *this % 12; // calls non-member operator %
        operator %( *this, 12 ); // error! 参数个数不匹配,因为调用的是X ::operator %( const X & )        
    }
    对于infix operator call,成员函数和非成员函数编译器都会考虑到。因此对operator %进行infix call时,会找到 non-member operator。这并不属于overloading,只是一种不同的查找算法。调用non-infix call 函数operator %( *this, 12 )时,遵循标准函数查找规则,于是找到成员函数,发生了参数个数不正确的错误。

    为了效率,infix calls 使用了一种退化的ADL:在决定overload哪一个函数时,只考虑infix operator的左边参数类域和全局域。 ADL包括了由参数所引入的namespaces 域。

  • 相关阅读:
    再谈 Devstack(Rocky)
    记一次性能测试与优化经历
    Nova rebuild for boot from volume issue
    OpenStack RPM Sample 解析
    [Cinder] 存储 Qos
    Octavia Rocky UDP 负载均衡功能试验
    Keepalived + LVS-NAT 实现高可用四层 TCP/UDP 负载均衡器
    LVS 四层 TCP/UDP 负载均衡器
    集群的定义以及类别定义
    对程序员又了解了一些
  • 原文地址:https://www.cnblogs.com/aiwz/p/6333228.html
Copyright © 2011-2022 走看看