On Sat, 13 May 2000, Shaul Karl wrote:

> Consider the following C++ program:

[program moved to the end of this letter]

> Although I was able to compile it without warnings I do not understand:
> 1) What is the meaning of the first (left most) const in
>       const Integer operator+(const Integer& rv) ... ?  

it means that the '+' operator returns an 'r' value, not an 'l' value
(that is, you cannot assign into the integer it returns). look at
stroustroup for more info, or better yet - don't use operator overloading
when you can avoid that.

> 2) Isn't i a private member of Integer? Then howcome it can be accessed with 
> an expression like rv.i?

'rv.i' is accessed inside a method of the 'Interger' class. any method of
a class may access the private members of any objects of that same lass,
no matter of this object was received as an implicit parameter of the
method (i.e. this) or as an explicit parameter of the method, or it was a
global variable, static variable, etc.

guy

"For world domination - press 1,
 or dial 0, and please hold, for the creator." -- nob o. dy

> //: C12:OperatorOverloadingSyntax.cpp
> // From Thinking in C++, 2nd Edition
> // Available at http://www.BruceEckel.com
> // (c) Bruce Eckel 2000
> // Copyright notice in Copyright.txt
> #include <iostream>
> using namespace std;
> 
> class Integer {
> int i;
> public:
> Integer(int ii) : i(ii) {}
> const Integer
> operator+(const Integer& rv) const {
>   cout << "operator+" << endl;
>   return Integer(i + rv.i);
> }
> Integer&
> operator+=(const Integer& rv) {
>  cout << "operator+=" << endl;
>   i += rv.i;
>   return *this;
> }
> };
> 
> int main() {
> cout << "built-in types:" << endl;
> int i = 1, j = 2, k = 3;
> k += i + j;
> cout << "user-defined types:" << endl;
> Integer ii(1), jj(2), kk(3);
> kk += ii + jj;
> } ///:~



=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]

Reply via email to