Discussion:
Disambiguating between const and non-const member
(too old to reply)
Martin Magnusson
2007-07-04 12:56:58 UTC
Permalink
Suppose I have a class like this one:

class A
{
int* getint()
{
return const_cast<int*>(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?
Robert Bauck Hamar
2007-07-04 13:10:30 UTC
Permalink
Post by Martin Magnusson
class A
{
int* getint()
{
return const_cast<int*>(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.
Yes.
Post by Martin Magnusson
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?
Convert this to a const A*, and call getint() for that pointer:
const A* p = this;
return const_cast<int*>(p->getint());
--
rbh
S S
2007-07-04 19:02:09 UTC
Permalink
Post by Martin Magnusson
class A
{
int* getint()
{
return const_cast<int*>(getint());
return const_cast<int*>(static_cast<const A&>(*this)->getint());
Post by Martin Magnusson
}
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?
Loading...