Overriding an operator using const for both parameters in C ++

I am trying to create an overridden operator function using both constant parameters, but I cannot figure out how. Here's a simple example:

class Number
{
    Number()
    {
        value = 1;
    };

    inline Number operator + (const Number& n)
    {
        Number result;

        result.value = value + n.value;
        return result;
    }

    int value;
}

      

What I'm trying to do here is to pass two arguments to the addition function, which are constants and return the result without changing anything in the class:

const Number a = Number();
const Number b = Number();
Number c = a + b;

      

Is this possible and how can I do it?

Thank,

Dan

0


source to share


4 answers


inline

is understood in class declarations, so you don't need to specify it.

Idiomatically, you would make operator+

a non-member function declared outside of the class definition, for example:

Number operator+( const Number& left, const Number& right );

      

You might need a friend

class if it needs access to internals Number

.

If you must have it as a member function, you need to make the function const itself:

Number operator+( const Number& n ) const
{ // ...

      

For classes like this Number

, operator+

it is usually implemented in terms operator+=

as usual, you want all the usual operators to work as expected, and operator+=

it is generally easier to implement, rather operator+

than tends to lose any efficiency compared to implementing it separately.



Inside the class:

Number& operator+=( const Number& n );

      

Out of class:

Number operator+( const Number& left, const Number& right )
{
    return Number( left ) += right;
}

      

or even:

Number operator+( Number left, const Number& right )
{
    return left += right;
}

      

+7


source


class Number
{
    Number()
    {
        value = 1;
    };

    inline Number operator + (const Number& n) const
    {
        Number result;

        result = value + n.value;
        return result;
    }

    int value;
}

      



+1


source


What about:

inline Number operator + (const Number& n) const

      

+1


source


While I feel like the previous answers are good enough, I believe some clarification is needed.

Operators come (usually) in two flavors

The first is a non-member function, the second is a member function whose parameter is the "valid operand" of the operation and which usually returns the currently modified object.

For example, imagine an operator §

for a class T

. It can be written as a non-member function :

T operator § (const T & lhs, const T & rhs)
{
   T result ;
   // do the lhs § rhs operation, and puts the result into "result"
   return result ;
}

      

or as a member function :

T & T::operator § (const T & rhs)
{
   // do the "this § rhs" operation, and puts the result into "this"
   return *this ;
}

      

or even (very unusual) as another member function :

T T::operator § (const T & rhs) const
{
   T result ;
   // do the "this § rhs" operation, and puts the result into "result"
   return result ;
}

      

Usually, you prefer a function other than a member, if only because you shouldn't declare it a friend. Thus, using a non-member non-friend function increases the encapsulation of your object.

Disclaimer: There are other options, but I limit myself to arithmetic operators such as +

, *

, /

, -

etc. here as well as the "reliable" operator prototypes.

Analyze operator usage

In case +

:

  • Each operand should be constant as a = b + c

    it shouldn't change b

    , not c

    .
  • You can accumulate +

    like in a = b + c + d + e

    , so temporary files must exist.
T operator § (const T & lhs, const T & rhs)
{
   T result ;
   // do the lhs § rhs operation, and puts the result into "result"
   return result ;
}

      

In case +=

:

  • You know that the left operand of A (from A + = B) will be changed.
  • You know that the left operand of A (from A + = B) is its own result.

So, you have to use:

T & T::operator += (const T & rhs)
{
   // do the "this § rhs" operation, and puts the result into "this"
   return *this ;
}

      

As always, premature optimization is the root of all evil

I've seen code like this in production code, so it does :

T & operator + (const T & lhs, const T & rhs)
{
   static T result ; // result is STATIC !!!!
   // do the lhs + rhs operation, and puts the result into "result"
   return result ;
}

      

The author hoped to save one temporary. With this kind of code, writing a = b + c + d

leads to interesting (and incorrect) results.

^ _ ^

Last but not least

I wrote a list of operator prototype overloads on this page . The page is still under construction, but its main use (easy to copy / paste working prototypes) can be quite useful ...

+1


source







All Articles