about overloaded functions
Jun 30, 2011 at 9:46am UTC
hi:
I have a code below about overloaded functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#include<iostream>
using namespace std;
class Integer
{
long i;
Integer * This() {return this ;}
public :
Integer(long ll = 0):i(ll){ cout << "Integer constructor" <<endl;}
Integer(const Integer & h):i(h.i){ cout << "copy constructor" <<endl;}
friend ostream & operator << (ostream & o,const Integer & a);
friend const Integer & operator + (const Integer &a);
friend const Integer operator - (const Integer &a);
friend const Integer operator ~ (const Integer &a);
friend Integer * operator & ( Integer &a);
};
const Integer & operator + (const Integer &a)
{
cout << "+Integer" <<endl;
return a;
}
const Integer operator - (const Integer &a)
{
cout << "-Integer" <<endl;
return Integer(-a.i);
}
const Integer operator ~ (const Integer &a)
{
cout << "~Integer" <<endl;
return Integer(~a.i);
}
Integer * operator & ( Integer &a)
{
cout << "&Integer" <<endl;
return a.This();
}
ostream & operator << (ostream & o,const Integer & a)
{
return o << a.i ;
}
void f(Integer a)
{
cout << -a <<endl;
cout << +a <<endl;
cout << ~a <<endl;
cout << &a <<endl;
}
int main()
{
Integer a(1);
f(a);
}
I have a question if I change the code
friend Integer * operator & ( Integer &a);
into this
friend Integer * operator & ( const Integer &a);
compile would report a error;
so I don't how to and when to use the const correctly.the const means what here?
Last edited on Jun 30, 2011 at 9:54am UTC
Jun 30, 2011 at 10:55am UTC
compile would report a error
sigh, and the error is ...
The const mean that the object is constant, it can't change its state.
Jun 30, 2011 at 12:00pm UTC
If you provide a const object you must return a pointer to a const object. The same with non const. You can provide both functions.
Jul 1, 2011 at 12:52am UTC
compile would report a error
sigh, and the error is ..
error C2662: 'This' : cannot convert 'this' pointer from 'const class Integer' to 'class Integer &' Conversion loses qualifiers
Jul 1, 2011 at 12:56am UTC
If you provide a const object you must return a pointer to a const object.
I have tried to change the
Integer * This() {return this ;}
into
const Integer * This() {return this ;}
and change the
friend Integer * operator & ( Integer &a);
into
friend const Integer * operator & ( const Integer &a);
After writing the code and it being compiled I am prompted with the same error.
Last edited on Jul 1, 2011 at 1:00am UTC
Jul 1, 2011 at 1:12am UTC
The a is a const object, ie you cannot change it
but the This() function is not declared as such ,ie to the compiler it will change the object. Hence the problem. Since the This() function does not actually change the object make it const ie
Integer * This() const {return this;}
Topic archived. No new replies allowed.