Order of function match



 DEVELOP > c-Plus-Plus > Order of function match

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "ciccio"
Date: 25 Jan 2008 08:54:33 AM
Object: Order of function match
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
.

User: "Victor Bazarov"

Title: Re: Order of function match 25 Jan 2008 09:30:19 AM
ciccio wrote:

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
User: "James Kanze"

Title: Re: Order of function match 25 Jan 2008 01:19:57 PM
On Jan 25, 4:30 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:

ciccio wrote:

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.

The declaration is legal. A definition wouldn't be, nor would
be calling the function.

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.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.
User: "Victor Bazarov"

Title: Re: Order of function match 25 Jan 2008 02:14:02 PM
James Kanze wrote:

On Jan 25, 4:30 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:

ciccio wrote:

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER