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

D

Dave

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()
}
 
C

Claudio Jolowicz

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()
}
 
H

Howard

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
 
D

Dave

Howard said:
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...
 
O

Old Wolf

Dave said:
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 said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top