473,624 Members | 2,281 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 1787

"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*****@hotmai l.com> wrote in message
news:c6*******@ dispatch.concen tric.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_q ueue<>,
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.sup ernews.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<cons t 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
4642
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 = eval("document.imageForm.move" + ""); alert(moveElement); if (document.imageForm.select_all.checked) {
12
1877
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 language and the underlying database. We are in the process of exploring new languages to write our application in, as Progress is becoming expensive for us. Here are some of our issues 1. Progress is a 4GL language so it is very rapid development....
2
1933
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 | 15 | 122.165.177.211 | | 3 | 1 | 3 | 1 | 205.52.79.122 | | 4 | 7 | 3 | 9 | 122.165.177.211 |
5
4019
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 What I need to do is narrow this down into two fields:
1
2560
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. Below is my code. here strSelectSQL value is strSelectSQL = "Select emp.Empno, emp.FirstName, emp.LastName, emp.DB, emp.DOJ,emp.Grade,emp.yearofexperience,
0
1049
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 clicked, I have: row = ds.Tables.DefaultView.Find(xDate); dg.CurrentRowIndex = row; dg.Select(row); (row is an integer and, when a non-column header is clicked, is the index of that row. Above, I try to change row to the new location of xDate.)
3
15714
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 in USENET...get rewarded for it!
4
1216
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 user controls and highlight them or select them (like rubber band) by mouse-down-drag-mouse-up events. I am trying to capture coordinates of mouse down event on the panel holding the user controls. However, since panel is under the controls, I...
0
1235
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 deselected and the ForeColor turns black (even though it's still set to GrayText - stepped through to see if it was the case). If I select a different item instead of selecting nothing the color stays gray.. What would be causing this? Here's...
2
1108
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, new ListItem("Non Selected", "Non Selected"));
0
8177
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8681
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8341
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4183
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2611
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 we have to send another system
2
1488
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.