wim delvaux wrote:
class A {
public :
virtual void a( int x ) = 0;
virtual void a( void ) {
printf( "a without\n" );
}
};
class B : public A {
public :
virtual void a( int x ) { printf( "a with\n" ); }
};
int main( void ) {
B SomeB;
SomeB.a();
SomeB.a( 1 );
}
The error I get with gcc 3.4 is
t.cpp: In function `int main()':
t.cpp:25: error: no matching function for call to `B::a()'
t.cpp:17: error: candidates are: virtual void B::a(int)
What is going on ? Why can't I define two 'a' with different arguments ?
Once you've overloaded one version of "a", it masks all other versions
inherited from base classes, even if they have different signatures.
This can be a life-saver, since if you're overloading one version of a
method, there's a good chance you mean to overload others. You can tell
the compiler you really do want to use the base class definition for one
version, but the overloaded definition for another, with a "using
declaration", e.g. "using A::a;".
#include <cstdio>
using std::printf;
class A {
public :
virtual void a( int x ) = 0;
virtual void a( void ) {
printf( "a without\n" );
}
};
class B : public A {
public :
using A::a; // A "using declaration."
virtual void a( int x ) { printf( "a with\n" ); }
};
int main( void ) {
B SomeB;
SomeB.a();
SomeB.a( 1 );
}