I have code which is structured similar to the following:
#include <iostream>
class base
{
public:
void method();
};
class A:public base
{
public:
void method();
};
void base::method()
{
std::cout << "base method\n";
}
void A::method()
{
std::cout << "A method\n";
}
int main()
{
base* array[1];
array[0] = new A;
array[0]->method(); //should call A::method() but it doesnt.
delete array[0];
}
I would like to, whenever i call "method()" using the pointer array call the method belonging to the A class (or any other classes derived from base, pointed to by array[]).
However this code seems to always call the method attached to the class "base". From what I understand the "method()" defined by the class A should override the "method()" in the base class.
How do i call the method defined in "A" via an array of pointers of type "base"?
Thanks for any help.