[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Problem returning const pointer

kk_oop

10/22/2008 10:05:00 PM

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?

Thanks!

Ken


3 Answers

Pete Becker

10/22/2008 10:25:00 PM

0

On 2008-10-22 18:05:27 -0400, kk_oop@yahoo.com said:

> 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( );

myReturnClass is not a const pointer. Without seeing the definition of
myClass1 it's not possible to say what's going on. Which is why the FAQ
recommends including a minimal code sample that shows the problem,
rather than a summary that omits the key parts.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Thomas J. Gritzan

10/22/2008 11:21:00 PM

0

kk_oop@yahoo.com schrieb:
> Hi. I just wrote a function that returns a const pointer:
>
> MyClass
> {
> ...
> public:
> virtual ReturnClass * const getReturnClass( );
> ...
> }

Top-level const on return values is quite useless, since you make a copy
of the returned value anyway.

What do you want to accomplish by that?

> 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( );

myReturnClass is not const.

It's like this:

const int i_const = 5;
int i2 = i_const;
i2 = 7; // i2 is not const!

i2 contains a copy of i_const, but is not const itself.

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

Consider using references more and pointers less.

--
Thomas

James Kanze

10/23/2008 8:06:00 AM

0

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