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
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