Member function selection based on cv qualification

D

Dave

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)();
}
 
V

Victor Bazarov

Dave said:
#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)();
}

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
 

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,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top