typeid

Hi!

Why cant I access get and setRadius()?
And no, I dont want to use dynamic_cast.

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
28
29
30
31
32
#include "Circle.h"
#include "Rectangle.h"


int main()
{
	_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
	Shape * theArray[10];
	theArray[0]= new Circle (2,3,4.5);
	theArray[1]= new Rectangle(5,7,12.3,15.2);
	theArray[2]= new Rectangle(5,2,7.8,9.1);
	theArray[3]= new Circle(7,8,12.7);

	for(int i=0;i<=3;i++)
	{
		theArray[i]->print();
		cout<<endl;
	}
	for(int i=0;i<=3;i++)
	{
		double rad=0;
		if(typeid(*theArray[i])==typeid(Circle))
		{
			rad=(Circle*)theArray[i]->getRadius();
			(Circle*)theArray[i]->setRadius(rad*2);
		}
	}



	return 0;
}


best regards

And no, I dont want to use dynamic_cast.


Well, too bad. C style casts work in cases where you would use reinterpret, const or static casts, but NOT in cases where dynamic casts are needed.

http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=134
Last edited on
So you are saying that I'm not able to use typeid?
If so, could you please show me how dynamic_cast would work?
You can use type id, you just can't use a C-style cast for such cases.

1
2
3
4
5
if(typeid(*theArray[i])==typeid(Circle))
		{
			rad=dynamic_cast<Circle*>(theArray[i])->getRadius();
			dynamic_cast<Circle*>(theArray[i])->setRadius(rad*2);
		}
Last edited on
hm..ok.
Could you show me how It's supposed to be done?
That's what I just did?
oh sorry, thanks for the help!
Topic archived. No new replies allowed.