c++ const member function that returns a const pointer.. But what type of const is the returned pointer? -
i apologize if has been asked, how create member function in c++ returns pointer in following scenerios: 1. returned pointer constant, junk inside can modified. 2. junk inside constant returned pointer can modified. 3. neither junk, nor pointer can modified.
is so:
int *const func() const
const int* func() const
const int * const func() const
all of tutorials i've read don't cover distinction.
side note: if method declared const tutorials i'm stating won't modify parameters.. not clear enough me in case when parameter pointer. parameters need like:
a. void func(const int* const x) const;
b. void func(const int* x) const;
c. void func(const int* const x) const;
i don't know book have read, if mark method const means this
of type const myclass*
instead of myclass*
, in turn means cannot change nonstatic data members not declared mutable
, nor can call non-const methods on this
.
now return value.
1 . int * const func () const
the function constant, , returned pointer constant 'junk inside' can modified. however, see no point in returning const pointer because ultimate function call rvalue, , rvalues of non-class type cannot const
, meaning const
ignored anyway
2 . const int* func () const
this useful thing. "junk inside" cannot modified
3 . const int * const func() const
semantically same 2, due reasons in 1.
hth
Comments
Post a Comment