Question

I'm trying to create an overridden operator function using both const parameters, but I can't figure out how to do it. Here is 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 am trying to do here is pass in two arguments into the addition function that are both const 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 would I go about doing this?

Thanks,

Dan

Was it helpful?

Solution

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

Most idiomatically, you would make operator+ a non-member function declared outside the class definition, like this:

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

You might need to make it a friend of the class if it needs access to Number's internals.

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

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

For classes like Number, operator+ is typically implemented in terms of operator+= as usually you want all the usual operators to work as expected and operator+= is typically easier to implement and operator+ tends not to lose any efficiency over implementing it separately.

Inside the class:

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

Outside the 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;
}

OTHER TIPS

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

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

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

    int value;
}

How about:

inline Number operator + (const Number& n) const

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

Operators come (usually) in two flavors

The first being the non-member functions, the second being the member function whose parameter is the "right operand" of the operation and which usually returns the current modified object.

For example, imagine there's an operator § for a class T. It could be written either 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 unusually) 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 should prefer the non-member function, if only because you should not declare it friend. Thus, using non-member non-friend function enhance the encapsulation of your object.

Disclaimer: There are other flavors, but I'm limiting myself to arithmetic operators like +, *, /, -, etc. here, as well as "credible" operator prototypes.

Analyse your use of the operator

In the case of +:

  1. Each operand needs to be constant because a = b + c must not change b, nor c.
  2. You can accumulate +, like in a = b + c + d + e, so temporaries 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 the case of +=:

  1. You know the left operand A (from A += B) will be modified.
  2. You know the left operand A (from A += B) is its own result.

So you should 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 this kind of code in production code so, it does happen:

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 economize one temporary. With this kind of code, writing a = b + c + d leads to interesting (and wrong) results.

^_^

Last but not least

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top