On Jul 4, 5:56 pm, Martin Magnusson <mar...@xx-blecket-xx.org> wrote:
> Suppose I have a class like this one:
>
> class A
> {
> int* getint()
> {
> return const_cast<int*>(getint());
return const_cast<int*>(static_cast<const A&>(*this)->getint());
> }
>
> const int* getint() const
> {
> //some complicated code
> return i;
> }
>
> int* i;
>
> };
>
> That is, I have a const member returning a const pointer, but I also
> want to be able to access a non-const pointer to the same data, without
> doing a lot of copy-paste coding. So I'd like to call the const function
> from the non-const function. For cleanliness, I'd like the functions to
> have the same name, and disambiguate between the two based on the
> context where they are called. This is generally not a problem, but with
> this implementation, the process gets stuck in an infinite recursive
> loop when the non-const method is called.
>
> Is there a way to call the const getint() from within the non-const
> getint(), or would I have to copy the body of the const method, or
> rename it const_getint, to get rid of the recursive behaviour?
|