Is it possible to call the virtual function foo( int ) from B without using what is done in comment ?
class A {
public:
virtual void foo ( char * ) {
}
virtual void foo ( int ) {
}
};
class B : public A {
public:
void foo ( char * ) {
}
//void foo ( int i ) {
//
// A::foo(i);
//}
};
B b;
b.foo(123); // cannot convert argument 1 from 'int' to 'char *'
A virtual function is a special type of function that, when called, resolves to the most-derived version of the function that exists between the base and derived class. This capability is known as polymorphism.
The main use of virtual function is to achieve Runtime Polymorphism. Runtime polymorphism can be achieved only through a pointer (or reference) of base class type.
A virtual function in C++ is a base class member function that you can redefine in a derived class to achieve polymorphism. You can declare the function in the base class using the virtual keyword.
Virtual Functions and Runtime Polymorphism in C++It tells the compiler to perform late binding where the compiler matches the object with the right called function and executes it during the runtime. This technique of falls under Runtime Polymorphism. The term Polymorphism means the ability to take many forms.
Yes, it is possible. The problem here is that the function B::foo(char*)
hides the name of the inherited function A::foo(int)
, but you can bring it back into scope of B
with a using
declaration:
class B : public A {
public:
void foo ( char * ) {
}
using A::foo;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With