Re: overriding base methods
The problem is int and pointer( no matter to what type ) is compliant types.
According this , the compiler cannot ajust which method you call , when you call returno1;
Another problem is ,
it is necessary to make B's destructor virtual
Re: overriding base methods
I have tried by changing the data types to long and string too.But I get the same error message.is there some other way to 'unhide' the base method?
Re: overriding base methods
What compiler are you using ?
I am using gcc ,
there is no errors.
Re: overriding base methods
that is overloading, not overriding.
Overriding means you have a virtual base function for which the derived class writes its own implementation.
Overloading means that the derived class writes a new function with the same name. This is done either when the base class method is non-virtual or when the derived class method has a different prototype (i.e. parameters and/or return type), or both.
There is one restriction - you cannot override (or attempt to overload) a virtual base function only by changing its return type.
For overloaded functions, like you have there, you can invoke the base-class function of the same name by scoping it, thus Base::function1()
from within the derived class or (<static_cast>(Base *)p)->function1() from outside. (cast to Base & if you have a ref/instance rather than a pointer).
Thus
class Base
{
public:
int function1( void *);
int function2( int );
};
class Derived : public Base
{
public:
void *function1( int )
{
int x = Base::function1( NULL );
return NULL;
}
void function2();
};
void main()
{
Derived o;
o.function1( 23 ); // ok
Base& br = o;
br.function2( 23 );
o.function2();
}
The best things come to those who rate