Selection of overloaded function

F

Fred

What does the standard say about which overloaded function to match in
this situation:

(Assume classes A and B are defined, and there is an overloaded
operator defined
const A& operator = (const void *);

class Base {
public:
A a;
B b;
virtual void fun( A a& );
virtual void fun( B *b, int i=0 );
};

class Sub : public Base {

void fun( A a);
void fun( B *b, int i=0);
};

Then somewhere I make this call:
A a = new A();
B b = new B();
Sub *s = new Sub();

s->fun( b );

Should it i:
1) invoke Sub's function fun(A)
2) Base's function fun(B,int) with i defaulted to 0
3) fail with no match

gcc gives me option 1

However, if I remove Sub's 2-argument version,
I get option (3)
 
M

Michael Tsang

Fred said:
What does the standard say about which overloaded function to match in
this situation:

(Assume classes A and B are defined, and there is an overloaded
operator defined
const A& operator = (const void *);

class Base {
public:
A a;
B b;
virtual void fun( A a& );
This is a syntax error. What do you mean?
 
P

Permostat

Fred said:
What does the standard say about which overloaded function to match in
this situation:

[Broken code snipped]
Should it i:
1) invoke Sub's function fun(A)
2) Base's function fun(B,int) with i defaulted to 0
3) fail with no match
gcc gives me option 1
However, if I remove Sub's 2-argument version,
I get option (3)

Your code is pretty broken unless I'm missing something basic. However
it may help you to understand name hiding.

class A { };
class B { };

struct Base {
   void foo(A&);

};

struct Derived : Base {
   void foo(B&);

};

int main() {
   A a;
   Derived d;
   d.foo(a); // will not compile, see below

}

"foo(A&)" is hidden and "foo(B&)" doesn't work for an argument of type
A. To fix this, you have to explicitly expose the Base function with
"using Base::foo;" in the Derived class. Like this:

struct Derived : Base {
   void foo(B&);
   using Base::foo;



};

You're obviously trying to hide rape pornography inside decoy files.
Since this is an illegal action - I will not provide help.

sperm-
 
F

Fred

This is a syntax error. What do you mean?

Oops - I meant A &a

The real question concerns what is the sequence used to match
overloaded and polymorphed functions.

It seems to be:
1) In Sub class, select the function that matches all non-defaulted
parameter types exactly.
2) If no exact match, see if one exists that can be matched by type-
conversion of non-defaulted parameters.
3) If still no match, repeat (1) and (2) but adding the defaulted
parameters.
4) If still no match (and there were ANY functions by that name
defined in Sub, fail with a 'no match found' error.
5) If there were no functions by that name defined in Sub, then and
only then look for a match in the superclass.
 
J

James Kanze

Oops - I meant A &a
The real question concerns what is the sequence used to match
overloaded and polymorphed functions.
It seems to be:
1) In Sub class, select the function that matches all non-defaulted
parameter types exactly.
2) If no exact match, see if one exists that can be matched by type-
conversion of non-defaulted parameters.
3) If still no match, repeat (1) and (2) but adding the defaulted
parameters.
4) If still no match (and there were ANY functions by that name
defined in Sub, fail with a 'no match found' error.
5) If there were no functions by that name defined in Sub, then and
only then look for a match in the superclass.

Not at all. The sequence is:

1. Use standard name lookup to find the matching name(s). If
any of the matching name is not a function or a function
template, it is an error.

2. Use ADL to find additional potential names; only functions
and function templates are considered.

3. Do template argument deduction on any function templates in
the resulting set. If deduction fails, throw the function
template out; if deduction is ambiguous, error; otherwise,
add the results of the deduction (which is a function) to
the overload set.

4. If any of the resulting functions has default arguments,
replace it with two or more instances, with and without the
default arguments.

5. Do overload resolution.
 

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,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top