Hi All
I have a question on dyanamic casting.
Below is the code:
This code is giving compile time error that:Code:#include<iostream> using namespace std; class base { public: virtual int get() { cout<<"Inside base"; return 1; } }; class derived : public base { public: int get() { cout<<"Inside derived"; return 1; } }; int main() { derived *b = new base(); b->get(); getchar(); return 0; }
I want to understand why? What I was thinking that since derived class is pointing to base class, which mean that we are going from specialized class to generalized one, so that should be legal. In other words, upcasting.cannot convert from 'class base *' to 'class derived *'
Can anybody help to understand this?
Thanks
Nickman