It seems like I have a confusion about polymorphism and/or pointers. Please see the simple code and the outputs below.
class Cell {
public:
virtual void call() { std::cout<<"I am a cell"<<std::endl; };
};
class Human: public Cell {
public:
void call() override { std::cout<<"I am a human"<<std::endl; };
};
int main()
{
Cell *c = new Cell();
Human h;
c = &h;
(*c).call(); // ->>>>>> First call
auto temp = *c;
temp.call(); // ->>>>>> Second call
auto temp2 = c;
temp2->call(); // ->>>>>> Third call
auto func1 = [&](Cell *x) {
x = &h;
return *x;
};
func1(c).call(); // ->>>>>> Fourth call
return 0;
}
The outputs are as follows:
I am a human
I am a cell
I am a human
I am a cell
Could someone please explain me what is going on here?