473,394 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Disambiguating between const and non-const member

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?
Jul 4 '07 #1
2 1393
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
Jul 4 '07 #2
S S
On Jul 4, 5:56 pm, Martin Magnusson <mar...@xx-blecket-xx.orgwrote:
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?

Jul 4 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Doug Fort | last post by:
This is an excerpt from a much longer post on the python-dev mailing list. I'm responding here, to avoid cluttering up python-dev. <snip> >Some English readers might not really imagine, but it...
12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
22
by: Steve - DND | last post by:
We're currently doing some tests to determine the performance of static vs non-static functions, and we're coming up with some odd(in our opinion) results. We used a very simple setup. One class...
8
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I...
0
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.