473,513 Members | 2,453 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Selecting const vs non-const methods of teh same name at will


Hello NG,

In a thread I had started a long time ago, the conclusion had been reached
that there is no good way to select between calling const vs non-const
methods of the same name. (Of course, the object in question is non-const
so that both may be legally called.) However, it appears I have indeed
found a way to do it. I'm sure hoping nobody can poke any holes in my
little scheme, but I want to throw it out there to see if it stands up to
scrutiny. Please see the sample program included below...

Thanks,
Dave
#include <iostream>

using namespace std;

class foo
{
public:
void f() {cout << "non-const" << endl;}
void f() const {cout << "const" << endl;}
};

int main()
{
void (foo::*pm1)() = &foo::f;
void (foo::*pm2)() const = &foo::f;

foo a;

(a.*pm1)(); // Call the non-const f()
(a.*pm2)(); // Call the const f()
}
Jul 22 '05 #1
4 1781

"Dave" <be***********@yahoo.com> wrote in message

It works fine for me in my tests, but I'm curious as to why you'd ever want
to have a const and a non-const version of a function with the same name. I
would think that a function which modifies an object would tend to have an
"action" name, like SetTime, or UpdateParams or something, while a const
function would be more like Top or GetName.

-Howard
Jul 22 '05 #2
On Wed, 21 Apr 2004, Dave wrote:
In a thread I had started a long time ago, the conclusion had been reached
that there is no good way to select between calling const vs non-const
methods of the same name. (Of course, the object in question is non-const
so that both may be legally called.) However, it appears I have indeed
found a way to do it.


[snip]

That's nice. Here's a variation on your idea:

#include <iostream>
using namespace std;

class foo
{
public:
void f() {cout << "non-const" << endl;}
void f() const {cout << "const" << endl;}
};

int main()
{
typedef void (foo::*pm1)();
typedef void (foo::*pm2)() const;

foo a;

(a.*static_cast<pm1>(&foo::f))(); // Call the non-const f()
(a.*static_cast<pm2>(&foo::f))(); // Call the const f()
}

--
Claudio Jolowicz
http://www.doc.ic.ac.uk/~cj603

Jul 22 '05 #3

"Howard" <al*****@hotmail.com> wrote in message
news:c6*******@dispatch.concentric.net...

"Dave" <be***********@yahoo.com> wrote in message

It works fine for me in my tests, but I'm curious as to why you'd ever want to have a const and a non-const version of a function with the same name. I would think that a function which modifies an object would tend to have an
"action" name, like SetTime, or UpdateParams or something, while a const
function would be more like Top or GetName.

-Howard


Actually, I don't want to name any of my methods with the same name. What I
want to do is parameterize a generic state space search library I'm writing
to use one of std::stack<>, std::queue<>, or std::priority_queue<>,
depending on the search technique to be used (std::stack<> results in
depth-first search, etc...). The problem is that the name of the method to
get the next element varies. For stack and priority_queue, it's top(). For
queue, it's front(). However, all of these methods have the same signature,
so I can use a pointer-to-member. That is as long as I select a const
method! priority_queue has only a const version, whereas stack and queue
have both const and non-const versions! So, I must be able to select either
at will. It was while working through all of this that I realized I could
use pointers to members to make my choice of which to call...
Jul 22 '05 #4
"Dave" <be***********@yahoo.com> wrote in message news:<10*************@news.supernews.com>...

In a thread I had started a long time ago, the conclusion had been reached
that there is no good way to select between calling const vs non-const
methods of the same name.
For certain definitions of 'good', I suppose
#include <iostream>

using namespace std;

class foo
{
public:
void f() {cout << "non-const" << endl;}
void f() const {cout << "const" << endl;}
};

int main()
{ foo a;
const foo &a_const = a;
a.f(); // Call the non-const f
a_const.f(); // Call the const f

Or if you don't want to name the reference:
const_cast<const foo &>(a).f(); // Call the const f
void (foo::*pm1)() = &foo::f;
void (foo::*pm2)() const = &foo::f;

foo a;

(a.*pm1)(); // Call the non-const f()
(a.*pm2)(); // Call the const f()
}


What was the problem you were trying to avoid, exactly?
Jul 22 '05 #5

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

Similar topics

9
4637
by: Phil Powell | last post by:
I am producing a form using PHP on the back end and Javascript on the front end. The resulting script will come to the browser as follows: <script> <!-- function selectAll() { moveElement =...
12
1856
by: mpinsley | last post by:
We are a software company that provides Inventory & Procurement mangement to the hospitality industry. For the past twenty years we have been using Progress Software as both the development...
2
1930
by: Zaphod | last post by:
I have a table with 5 fields, of which several sometimes have duplicates. example; | id | item | day | hour | ip_address | | 1 | 3 | 2 | 11 | 204.156.33.78 | | 2 | 7 | 2 ...
5
4010
by: Jagdip Singh Ajimal | last post by:
I have 6 columns, all with dates within them, i.e. Proposed Start Date 1 Proposed Start Date 2 Proposed Start Date 3 Proposed Finish Date 1 Proposed Finish Date 2 Proposed Finish Date 3 ...
1
2553
by: Ramesh | last post by:
hi, I am selecting fields from three table for manupulating data and i want to display total number of records selected. But i am always getting -1 value, eventhough 1000 of records are selected....
0
1044
by: melanieab | last post by:
Hi, I'm trying to re-select the currently selected row after a DataGrid sort. When a row is selected, I get the unique string xDate that's associated with that row. When the Column Header is...
3
15704
by: larry mckay | last post by:
anyone have the code to select and listview item or row (subitems) after a doubleclick event from a listview. *** Sent via Developersdex http://www.developersdex.com *** Don't just participate...
4
1213
by: Dino M. Buljubasic | last post by:
I have a form and a panel on the form that holds 24 user controls each one representing an hour (0 - 23). The panel is completelly covered with the user controls. I need to be able to select...
0
1217
by: Benny Raymond | last post by:
I have a list view and a button that turns the currently selected item's ForeColor to SystemColors.GrayText The problem I'm having is that when the user clicks in white space, the item becomes...
2
1105
by: sck10 | last post by:
Hello, I am using the following and am trying to figure out how to select the nth item of a list in a DropDownList. //Insert and Add items to the DropDownList ddlPayStatus.Items.Insert(0,...
0
7166
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
7386
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,...
1
7106
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...
0
5689
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3236
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3226
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1601
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
459
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.