casting is not working please help


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <conio.h>
using namespace std;
class base{
public:
	void print(){cout<<"I'm Base"<<endl;}


};
class drived:public base{
public:
	void print(){cout<<"I'm Drived"<<endl;}


};

int main()
{
	base* b ;
	drived* d= new drived;
	b = d;
	b = static_cast<drived*>(b);
	
	b->print();
	_getch();
}

at line number 24 the print method of base class is called instead of drived class
The type of b is base even after the casting. So function print of base class will be called.
I think you meant something as

drived *d2 = static_cast<drived*>(b);
d2->print();
Last edited on
Topic archived. No new replies allowed.