Richard Ellingworth(deleted)
|
Problem with method overloading
|
Richard Ellingworth(deleted)
06/25/2008 10:54 AM
post9671
|
Problem with method overloading
Why won't the following code compile?
=========================================
#include <stdio.h>
class Base {
public:
virtual void Wibble( int i ) = 0;
void Wibble( int *i ) {
Wibble( *i );
};
};
class Derived : public Base {
public:
void Wibble( int i ) {
printf( "%d ", i );
}
};
int main( int argc, char *argv[] ) {
Derived der;
int i = 10;
der.Wibble( &i );
return 0;
}
=========================================
I get two errors:
error: initializing argument 1 of 'virtual void Derived::Wibble(int)'
error: invalid conversion from 'int*' to 'int'
So it looks like the Derived class has not inherited the second overload of Wibble. Why not?
Two things fix it:
1) Rename the second overload to something like WibbleWobble. Then the compiler can find it OK.
2) An overload that does not involve a pointer seems to work OK. For example Wibble( float f )
Is there a switch I need to throw to make overloading work properly? Otherwise it looks like the compiler cannot cope
with overloads involving pointers.
I am using Momentics 6.3.2 with GNU Compiler Collection (3.3.5).
|
|
|