Pointers to functions: How is the proper function selected?

A

Aguilar, James

I want to know how C++ selects the correct function when there are two
functions of the same name which might be selected.

Here is an example.

class Foo
{
public:
void fooBar(); //(m1)
void fooBar(int); //(m2)
}

int main()
{
void (*ptrToMeth)(int) = Foo::fooBar; //(1)
void (*ptrToMeth2)() = Foo::fooBar; //(2)

return 0;
}

I know that the pointer to fooBar is resolved based on its name. However,
how do the assignments (1) and (2) know that they are supposed to point to
(m2) and (m1) respectively?

Thanks for the help in advance.
 
P

Phlip

int main()
{
void (*ptrToMeth)(int) = Foo::fooBar; //(1)
void (*ptrToMeth2)() = Foo::fooBar; //(2)
I know that the pointer to fooBar is resolved based on its name. However,
how do the assignments (1) and (2) know that they are supposed to point to
(m2) and (m1) respectively?

I adjust your code to something I suspect has better odds of compiling. No
class in the way to bring "pointers to member functions" into the question:

void fooBar(); //(m1)
void fooBar(int); //(m2)

void (*ptrToMeth)(int) = fooBar; //(1)
void (*ptrToMeth2)() = fooBar; //(2)

The answer is this: That little 'int' reaches an invisible arm out to the
right, passed the = sign, grabs that 'fooBar', and twists it. The 'fooBar'
is really a little list of 'fooBar' overloads, and the 'int' picks the
instance it matches.

From here, grab the C++ Faq (Google for it) and look up "member function
pointers". After you learn them, you can ask the same question, and they'l
have generally the same answer.
 
A

Aguilar, James

Phlip said:
Aguilar, James wrote:

[snip]

Sweet action. I had thought it might be something like this, but I could
not find anything about it on the website. I'm going to look up the member
functions on the FAQ now! Thanks much!
 
J

John Harrison

I want to know how C++ selects the correct function when there are two
functions of the same name which might be selected.

By examining the type of the variable being assigned to is the short
answer.
Here is an example.

class Foo
{
public:
void fooBar(); //(m1)
void fooBar(int); //(m2)
}

int main()
{
void (*ptrToMeth)(int) = Foo::fooBar; //(1)
void (*ptrToMeth2)() = Foo::fooBar; //(2)

return 0;
}

I know that the pointer to fooBar is resolved based on its name.
However,
how do the assignments (1) and (2) know that they are supposed to point
to
(m2) and (m1) respectively?

Thanks for the help in advance.

Your code is illegal. Perhaps you mean this?

class Foo
{
public:
void fooBar(); //(m1)
void fooBar(int); //(m2)
};

int main()
{
void (Foo::*ptrToMeth)(int) = &Foo::fooBar; //(1)
void (Foo::*ptrToMeth2)() = &Foo::fooBar; //(2)
return 0;
}

C++ allows this in several contexts (this is from 13.4 para 1 if
C++ standard)

a) object or reference being initalised
b) left hand side of assignment
c) function parameter
d) user defined operator parameter
e) return value
f) explicit type conversion
g) non-type template parameter

Hard to imagine any other situations in which you would want to do this.

AFAIK exactly one of the overloaded functions must match exactly the type
of the target.

john
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top