overwriting member with differing parameter types

H

Harald Finster

Hello,

sorry, this is probably a really dumb question, but I don't
understand the behavior of the g++ compiler at the moment.


The following snippet

-------- Test.cpp -----

#include <iostream>

class Base {

public:

void foo( int i ) {
std::cerr << "foo(int)" << std::endl;
}
};



class Derived : public Base {

public:

void foo( const char * i ) {
std::cerr << "foo(char*)" << std::endl;
}
};


int main( int argc, char ** argv ) {
Derived d;
d.foo("Hello");
d.foo(123);
}

--------

results in the following error:

g++ Test.cpp
Test.cpp: In function ‘int main(int, char**)’:
Test.cpp:30: error: invalid conversion from ‘int’ to ‘const char*’
Test.cpp:30: error: initializing argument 1 of ‘void
Derived::foo(const char*)’

Seems, that the compiler does not take differing parameter types
of the member functions into account. My understanding was, that
member-functions are regarded as 'different' if the names and/or
parameter types differ. (Only the return type is ignored.)

I expected, that Derived inherits foo(int) from Base and
thus has the member functions:
foo(int)
foo(const char*)

Instead foo(const char*) seems to overwrite foo(int)

Am I wrong?

The same snipped works as expected (by me) with EVC for example.

Thanks for any feedback - I am really confused :~{

Kind regards

Harald
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top