Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Disambiguating between const and non-const member

Reply
Thread Tools

Disambiguating between const and non-const member

 
 
Martin Magnusson
Guest
Posts: n/a
 
      07-04-2007
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?
 
Reply With Quote
 
 
 
 
Robert Bauck Hamar
Guest
Posts: n/a
 
      07-04-2007
Martin Magnusson wrote:

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


Yes.

> 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
 
Reply With Quote
 
 
 
 
S S
Guest
Posts: n/a
 
      07-04-2007
On Jul 4, 5:56 pm, Martin Magnusson <(E-Mail Removed)> 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?



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Disambiguating separated node sets Andy Dingley XML 2 10-20-2005 08:16 PM
gettext and "disambiguating comments" Terry Hancock Python 1 07-11-2005 10:40 PM
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static' Santa C Programming 1 07-17-2003 02:10 PM



Advertisments