Order of function match

C

ciccio

Hi,

This is most likely a question asked very often, and I have been
searching for answers in several books and sites but not found.

Assume the following classes and following function.

class foo;
class bar {
public:
foo g(void) const;
foo &g(void);
}

void f(const &foo);

When I call the following

bar tmp;
f(tmp.g());

Which of the two will be called and why?

And the bigger question of this is, what is the general order of
function calls when they are overloaded?

Thanks for the help
 
V

Victor Bazarov

ciccio said:
This is most likely a question asked very often, and I have been
searching for answers in several books and sites but not found.

Assume the following classes and following function.

class foo;
class bar {
public:
foo g(void) const;

'foo' is an incomlete type. Cannot use it as a function return
value type.
foo &g(void);
}

void f(const &foo);

Syntax error. Reposition the ampersand.
When I call the following

bar tmp;
f(tmp.g());

Which of the two will be called and why?

Is 'tmp' const? For non-const object the non-const function
is called.
And the bigger question of this is, what is the general order of
function calls when they are overloaded?

The "order" is not of function calls, but of functions to call.
The functions are ranked according with the section 13 of the
Standard. Then the most viable function (if any) is called.

V
 
J

James Kanze

'foo' is an incomlete type. Cannot use it as a function return
value type.

The declaration is legal. A definition wouldn't be, nor would
be calling the function.
The "order" is not of function calls, but of functions to
call. The functions are ranked according with the section 13
of the Standard. Then the most viable function (if any) is
called.

Yes, but to answer what it *probably* the intended question: if
all other things are equal, the function with the least
qualifiers will be called. If two functions differ only in
const, for example, the non-const version will be preferred any
time it can be called.
 
V

Victor Bazarov

James said:
ciccio said:
This is most likely a question asked very often, and I have been
searching for answers in several books and sites but not found. [..]
And the bigger question of this is, what is the general order of
function calls when they are overloaded?
The "order" is not of function calls, but of functions to
call. The functions are ranked according with the section 13
of the Standard. Then the most viable function (if any) is
called.

Yes, but to answer what it *probably* the intended question: if
all other things are equal, the function with the least
qualifiers will be called. If two functions differ only in
const, for example, the non-const version will be preferred any
time it can be called.

I don't believe in *guessing* the intended question. I'd rather
answer the one asked. Overload resolution is not a simple matter
that can be described in one paragraph of text.

V
 

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,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top