How to call an operator as function in C++

后端 未结 6 2143
执念已碎
执念已碎 2021-02-19 11:04

I want to call a specific operator of specific base class of some class. For simple functions it\'s easy: I just write SpecificBaseClass::function( args );. How sho

相关标签:
6条回答
  • 2021-02-19 11:13

    There are two notions in C++: one is operator, and the other is operator-function. Operator informally is what is broadly called a built-in operator. You cannot call them in functional notation. Operator-function is what is called an overloaded operator which you can call as already is seen from many answers: that is, if @ is a member binary operator of class A, then someObjectOfTypeA.operarot@(someOtherObject), or if it is a freestanding function, then like operator@ (object1, object2)

    0 讨论(0)
  • 2021-02-19 11:14

    Can't you just say

    c = b + a;
    
    0 讨论(0)
  • 2021-02-19 11:15

    The operator is a nonstatic member function, so you could use

    a.A::operator+( b )
    

    However, for another class that defines operator+ as a static member function, what you tried would be correct. And a third class might make it a free function (arguably the best way), so B::operator+(a,b) and a.operator+(b) would both be incorrect and operator+(a,b) would be right.

    Generally it's best just to use operator syntax a+b unless you know exactly what class it is, and that its implementation will never change. In a template context, writing a+b is a must, and it's essentially impossible to take the address of the overload (the only task that requires naming it) without a lot of work.

    In your context (a comment to another answer mentions templates), the best solution is

    c = static_cast< A const & >( a ) + static_cast< A const & >( b );
    

    … the problem is solved by slicing the types to reflect the subproblem, not precisely naming the function you want.

    0 讨论(0)
  • 2021-02-19 11:16

    Why not:

    B c = a + b;
    
    0 讨论(0)
  • 2021-02-19 11:19

    Try with:

    a.operator+(b);
    
    0 讨论(0)
  • 2021-02-19 11:24

    The syntax of specifying a function as

    <Class>::<Method>
    

    is used when Method() is a static method in Class. If Method() is a instance method, then the invocation is on a class object.

    Class classObject;
    classObject.Method()
    

    In this particular case, the declaration and usage of the operator+() method does not match.

    So, one approach would be to make the operator+() method static. But, I think, operator methods can only be non-static member function or non-member function.

    With all these knowledge, here is a full illustrative program.

    #include <cassert>
    
    struct B {
        int num_;
    
        B(int num) : num_( num ) {
        }
    
        static B add( const B & b1, const B & b2 ) {
            return B( b1.num_ + b2.num_ );
        }
    
        B operator+( const B & rhs ) {
            return B( num_ + rhs.num_ );
        }
    };
    
    int main() {
        B a(2), b(3);
    
        B c = B::add( a, b );
        assert( c.num_ == 5 );
    
        B d = a + b;
        assert( d.num_ == 5 );
    }
    

    The add() function is for example only, don't do it though.

    0 讨论(0)
提交回复
热议问题