James Kanze
10/23/2008 8:06:00 AM
On Oct 23, 12:05 am, kk_...@yahoo.com wrote:
> Hi. I just wrote a function that returns a const pointer:
> MyClass
> {
> ...
> public:
> virtual ReturnClass * const getReturnClass( );
> ...
> }
> I then wrote some code that calls the function to make sure the
> pointer was really const:
> MyClass myClass1;
> ReturnClass * myReturnClass = myClass1.getReturnClass( );
> MyClass myClass2;
> myReturnClass = myClass2.getReturnClass( );
> I anticipated getting a compiler error when I attempted to
> reset myReturnClass, but this did not happen. The compiler
> let me change the value. What did I do wrong?
Why would you anticipate a compiler error? myReturnClass isn't
const, so there's no reason you shouldn't be able to modify it.
How is this any different from:
int const c = 43 ;
int i = c ;
For non-class types, the const keyword is ignored for rvalues;
an rvalue of a non-class type is never "cv-qualified". (For
class types, it is relevant, since you can call member functions
on an rvalue; if the rvalue is const, you can only call const
member functions on it.) The return value of a function is an
rvalue, so there's really no point in declaring it const. (Note
that except for calling a member function on it, there is no
possible way to modify an rvalue.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34