Reputation: 35443
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
Upvotes: 0
Views: 2850
Reputation: 83309
While I feel the previous answers are good enough, I believe some clarification is needed.
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.
In the case of +
:
a = b + c
must not change b
, nor c
.+
, 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 +=
:
So you should use:
T & T::operator += (const T & rhs)
{
// do the "this § rhs" operation, and puts the result into "this"
return *this ;
}
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.
^_^
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...
Upvotes: 1
Reputation: 791869
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;
}
Upvotes: 7
Reputation: 61138
How about:
inline Number operator + (const Number& n) const
Upvotes: 1
Reputation: 625077
class Number
{
Number()
{
value = 1;
};
inline Number operator + (const Number& n) const
{
Number result;
result = value + n.value;
return result;
}
int value;
}
Upvotes: 1