Hello,
Can I have runtime polymorphism using references. I knew that runtime
polymorphism could be obtained only by pointers, but now I have tried
this in Visual C++ 8.0:
#include <iostream>
using namespace std;
class Shape
{
public:
virtual void draw()
{
cout<<"Draw Shape"<<endl;
};
Shape()
{
cout<<"new Shape"<<endl;
}
virtual
~Shape()
{
cout<<"delete Shape"<<endl;
}
};
class Circle: public Shape
{
public:
void draw()
{
cout<<"Draw Circle"<<endl;
}
Circle()
{
cout<<"new Circle"<<endl;
}
~Circle()
{
cout<<"delete Circle"<<endl;
}
};
int main()
{
Circle c;
Shape &s = c;
s.draw();
return 0;
}
What I get is:
new Shape
new Circle
Draw Circle
delete Circle
delete Shape
So, can I have runtime polymorphism with references?
The same output happens in g++. (version 3.3.1)
Thanks,
Doina Babu