operator+ cant be constant

T

Taras_96

Hi all,

Jesse Liberty writes:

"Note that the String class provides the operator+. The designer of
the Employee class has
blocked access to the operator+ being called on Employee objects by
declaring that all the string
accessors, such as GetFirstName(), return a constant reference.
Because operator+ is not (and
can't be) a const function (it changes the object it is called on),
attempting to write the following will
cause a compile-time error:

String buffer = Edie.GetFirstName() + Edie.GetLastName();"

(the String class he refers to is a custom String class coded for
demonstration purposes)

I agree that the line will cause a compile time error because operator
+ isn't defined as a const method:

constString.operator+(anotherConstString) <- will not compile

What I don't understand is his statement saying that operator+ can't
be a const function.. how does operator+ change the object that it's
being called on? AFAIK, operator+ should return a new object which is
a concatenation of the two, and shouldn't effect either operand.

Taras
 
I

Ian Collins

Taras_96 said:
Hi all,

Jesse Liberty writes:

"Note that the String class provides the operator+. The designer of
the Employee class has
blocked access to the operator+ being called on Employee objects by
declaring that all the string
accessors, such as GetFirstName(), return a constant reference.
Because operator+ is not (and
can't be) a const function (it changes the object it is called on),
attempting to write the following will
cause a compile-time error:

String buffer = Edie.GetFirstName() + Edie.GetLastName();"

(the String class he refers to is a custom String class coded for
demonstration purposes)

I agree that the line will cause a compile time error because operator
+ isn't defined as a const method:

constString.operator+(anotherConstString) <- will not compile

What I don't understand is his statement saying that operator+ can't
be a const function.. how does operator+ change the object that it's
being called on? AFAIK, operator+ should return a new object which is
a concatenation of the two, and shouldn't effect either operand.
That depends how the operator is declared. The binary operator + can be
declared as either a member function, or a free function. If it is a
member function, the string object is the left hand side of the
operation that is the right hand side is added to the object the
operator is called on.

If the operator is a free function, a new object is returned.

A simple example:

struct X
{
int n;

X( int n = 0 ) : n(n) {}

X& operator+( const X& other )
{
n += other.n;
return *this;
}
};

X operator+( const X& lhs, const X& rhs )
{
return lhs.n+rhs.n;
}
 
K

Kai-Uwe Bux

Ian said:
That depends how the operator is declared. The binary operator + can be
declared as either a member function, or a free function. If it is a
member function, the string object is the left hand side of the
operation that is the right hand side is added to the object the
operator is called on.

If the operator is a free function, a new object is returned.

A simple example:

struct X
{
int n;

X( int n = 0 ) : n(n) {}

X& operator+( const X& other )
{
n += other.n;
return *this;
}
};

X operator+( const X& lhs, const X& rhs )
{
return lhs.n+rhs.n;
}

Could it be that you are confusing operator+ and operator+= ?

I dont' see any reason why a member operator+ should not be const.


Best

Kai-Uwe Bux
 
I

Ian Collins

Kai-Uwe Bux said:
Could it be that you are confusing operator+ and operator+= ?

I dont' see any reason why a member operator+ should not be const.
I did, good spot.
 
R

Ralph

I agree that the line will cause a compile time error because operator +
isn't defined as a const method:

constString.operator+(anotherConstString) <- will not compile

What I don't understand is his statement saying that operator+ can't be
a const function.. how does operator+ change the object that it's being
called on? AFAIK, operator+ should return a new object which is a
concatenation of the two, and shouldn't effect either operand.

From looking at the source in his book (Teach Yourself C++ in 21 days,
right??) the implementation of operator+ does not change the object at
all so it can (and should) be const.

Btw. as his proposed 'fix' is to have Employee::GetFirstName() to return
a non const reference to a member variable and seeing some of the example
code in his book I wont trust the things he writes in his book too
much... So, my guess: you are right, he is wrong.

Ralph.
 
A

Andre Kostur

That depends how the operator is declared. The binary operator + can be
declared as either a member function, or a free function. If it is a
member function, the string object is the left hand side of the
operation that is the right hand side is added to the object the
operator is called on.

If the operator is a free function, a new object is returned.

A simple example:

struct X
{
int n;

X( int n = 0 ) : n(n) {}

X& operator+( const X& other )
{
n += other.n;
return *this;
}
};

And this is evil. operator+() (assuming "normal" semantics for operator+)
has no business modifying n. This isn't operator+=() !

I'd argue that operator+() _should_ be declared const.
 
T

Taras_96

From looking at the source in his book (Teach Yourself C++ in 21 days,
right??) the implementation of operator+ does not change the object at
all so it can (and should) be const.

Btw. as his proposed 'fix' is to have Employee::GetFirstName() to return
a non const reference to a member variable and seeing some of the example
code in his book I wont trust the things he writes in his book too
much... So, my guess: you are right, he is wrong.

Ralph.

:) Thanks Ralph
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,490
Latest member
Finplus

Latest Threads

Top