Connecting Tech Pros Worldwide Forums | Help | Site Map

Member function selection based on cv qualification

Dave
Guest
 
Posts: n/a
#1: Jul 23 '05
Hello all,

Please see the question in comment form in the program below.

Thanks!
Dave


#include <iostream>

using namespace std;

class foo_t
{
public:
void mem_fun()
{
cout << "No cv qualifications" << endl;
}

void mem_fun() const
{
cout << "const" << endl;
}

void mem_fun() volatile
{
cout << "volatile" << endl;
}

void mem_fun() const volatile
{
cout << "const volatile" << endl;
}
};

int main()
{
foo_t foo;

// How does the compiler decide which member function the initializer
// below references? This is well-formed!
void (foo_t::* ptr)() = &foo_t::mem_fun;

(foo.*ptr)();
}



Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Member function selection based on cv qualification


Dave wrote:[color=blue]
> #include <iostream>
>
> using namespace std;
>
> class foo_t
> {
> public:
> void mem_fun()
> {
> cout << "No cv qualifications" << endl;
> }
>
> void mem_fun() const
> {
> cout << "const" << endl;
> }
>
> void mem_fun() volatile
> {
> cout << "volatile" << endl;
> }
>
> void mem_fun() const volatile
> {
> cout << "const volatile" << endl;
> }
> };
>
> int main()
> {
> foo_t foo;
>
> // How does the compiler decide which member function the initializer
> // below references? This is well-formed!
> void (foo_t::* ptr)() = &foo_t::mem_fun;
>
> (foo.*ptr)();
> }[/color]

AFAIK, the type of the pointer to which the pointer to overloaded function
is assigned participates in the decision making. Since 'ptr' type doesn't
include any qualifiers, the unqualified function is used. Change your
program to be

...
int main()
{
foo_t foo;
void (foo_t::*ptr)() const = &foo_t::mem_fun;
(foo.*ptr)();
}

and you will see the difference.

Victor
Closed Thread