Overloading problem

M

mg.conti

Hi,

with this code:


class Base {
public:
virtual int Simple(int i, int j);
virtual int Simple(int i);
virtual ~Base(); // make compiler happy
};

class Derived : public Base {
public:
virtual int Simple(int i);
};

int
main(int argc, char* argv[])
{
Derived b;
b.Simple(1, 2);
}

Compiling with gcc 4.1.1 the call b.Simple(1,2) in main is not
resolved, while removing the declaration of Simple() in class Derived
all works.
Why the compiler don't see the Simple() with 2 parameter defined in
Base?

Thanks,
Marco
 
B

Barry

Hi,

with this code:


class Base {
public:
virtual int Simple(int i, int j);
virtual int Simple(int i);
virtual ~Base(); // make compiler happy
};

class Derived : public Base {
public:
virtual int Simple(int i);
};

int
main(int argc, char* argv[])
{
Derived b;
b.Simple(1, 2);
}

Compiling with gcc 4.1.1 the call b.Simple(1,2) in main is not
resolved, while removing the declaration of Simple() in class Derived
all works.
Why the compiler don't see the Simple() with 2 parameter defined in
Base?

First of all, this is NOT overloading, it's overriding.
int Derived::simple(int); *overrides* int Base::simple(int);
meanwhile it *hides* int Base::simple(int, int);
 
V

Victor Bazarov

Barry said:
Hi,

with this code:


class Base {
public:
virtual int Simple(int i, int j);
virtual int Simple(int i);
virtual ~Base(); // make compiler happy
};

class Derived : public Base {
public:
virtual int Simple(int i);
};

int
main(int argc, char* argv[])
{
Derived b;
b.Simple(1, 2);
}

Compiling with gcc 4.1.1 the call b.Simple(1,2) in main is not
resolved, while removing the declaration of Simple() in class Derived
all works.
Why the compiler don't see the Simple() with 2 parameter defined in
Base?

First of all, this is NOT overloading, it's overriding.
int Derived::simple(int); *overrides* int Base::simple(int);
meanwhile it *hides* int Base::simple(int, int);

Isn't this already in the FAQ?

V
 
B

Barry

Victor said:
Barry said:
Hi,

with this code:


class Base {
public:
virtual int Simple(int i, int j);
virtual int Simple(int i);
virtual ~Base(); // make compiler happy
};

class Derived : public Base {
public:
virtual int Simple(int i);
};

int
main(int argc, char* argv[])
{
Derived b;
b.Simple(1, 2);
}

Compiling with gcc 4.1.1 the call b.Simple(1,2) in main is not
resolved, while removing the declaration of Simple() in class Derived
all works.
Why the compiler don't see the Simple() with 2 parameter defined in
Base?
First of all, this is NOT overloading, it's overriding.
int Derived::simple(int); *overrides* int Base::simple(int);
meanwhile it *hides* int Base::simple(int, int);

Isn't this already in the FAQ?

Having problem accessing the website here
 
R

Rolf Magnus

Barry said:
Having problem accessing the website here

Me too. But my guess would be that it is in there. After all, this is a
question asked about every other week. If it isn't there, it definitily
should be.
 
R

Ron Natalie

Compiling with gcc 4.1.1 the call b.Simple(1,2) in main is not
resolved, while removing the declaration of Simple() in class Derived
all works.
Why the compiler don't see the Simple() with 2 parameter defined in
Base?

Because it never sees it.

Here's Ron's simple rules for function calling in C++:

1. First the name is looked up.
2. Then overloads for that name are checked.
3. Then the access is checked for the selected overload.
4. Then if the function is virtual, the final overrider is substituted.

In your case step 1 looks up Simple in Derived and finds the
NAME Derived::Simple.

There is exactly one overload for Derived::Simple, the Simple(int).

Defining a name in a derived class hides the name in the base class.

you can bring forward the names into the derived scope by adding

using Base::Simple;

to derived.
 
J

James Kanze

Here's Ron's simple rules for function calling in C++:
1. First the name is looked up.
2. Then overloads for that name are checked.
3. Then the access is checked for the selected overload.
4. Then if the function is virtual, the final overrider is substituted.

That's the way I always thought of it too. Until ADL.
Traditionally, the first rule read: "the name is looked up until
a scope is found which contains it", and the second said
"overload resolution is applied on all of function declarations
in the scope found in 1". In current C++, ADL will kick in in
1, causing (possibly) several different scopes to be considered
in 2. (Not that ADL is relevant in the example given.)

And of course, for templates, step one takes may take place
twice, in different contexts and using different look-up rules,
depending on whether the name is dependent or not.
 
B

Barry

James said:
That's the way I always thought of it too. Until ADL.
Traditionally, the first rule read: "the name is looked up until
a scope is found which contains it", and the second said
"overload resolution is applied on all of function declarations
in the scope found in 1". In current C++, ADL will kick in in
1, causing (possibly) several different scopes to be considered
in 2. (Not that ADL is relevant in the example given.)

And of course, for templates, step one takes may take place
twice, in different contexts and using different look-up rules,
depending on whether the name is dependent or not.

And SFINAE like enable_if also has effect here.
 

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
474,201
Messages
2,571,048
Members
47,649
Latest member
MargaretCo

Latest Threads

Top