Problem returning const pointer

K

kk_oop

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
 
T

Thomas J. Gritzan

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

James Kanze

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top