I Need Help With Dynamic_Casting

I do not understand dynamic_cast.
I have been reading this tutorial and I can not get the hang of it.
http://www.cplusplus.com/doc/tutorial/typecasting/
I Keep asking myself:
#1 Why do I need dynamic_cast
#2 What does it do. (I know it converts pointers to another type pointer but can't you do that already?)
Any explanations would help A LOT.
What helped you learn dynamic_cast?
Thank You for your time. :)
Last edited on
Hi

Do you already have a handle on polymorphism?

Andy
Yes. I understand Polymorphism very much.
Last edited on
Cool!

The purpose of both static_cast and dynamic_cast is to support safe up casting (and down casting) for polymorphic types.

The static_cast will always perform the cast; it trusts you to give it a valid pointer.

The dynamic_cast uses RTTI to check that the case it valid for a given pointer. If it's not valid, it returns a null pointer.

Both can also be used with references; as you can't return a "null ref" (?), an attempt at an invalid dynamic cast causes a bad_cast exception to be raised.

Andy
Oh so you mean this?
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
33
34
#include <iostream>
using namespace std;
class CPoly
{
protected:
	int Width , Height;
public:	
	void SetValues (int A , int B)
	{
		Width = A;
		Height = B;
	}
	virtual int Area ()
	{
		return 0;
	}
};
class CRect : public CPoly
{
public:
	int Area ()
	{
		return Width * Height;
	}
};
int main ()
{
	CRect MyRect;
	CPoly * PPoly = dynamic_cast <CPoly*> (&MyRect);
	PPoly->SetValues (6 , 7);
	cout << MyRect.Area () << endl;
	cin.get ();
	return 0;
}

Can you not do that without the dynamic_cast ?
No, it's to access a method which is declared in in a derived class, but not the base. But you can also use it to make it clear that you mean to down cast, though it's not actually needed that direction.

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
using namespace std;
class CPoly
{
protected:
	int Width , Height;
public:
	CPoly();
	...	
	CPoly(int A , int B);
	...
	void SetValues (int A , int B)
	{
		Width = A;
		Height = B;
	}
	virtual int Area () const = 0; // an unknown polygon cannot know what it's area is
};
class CRect : public CPoly
{
public:
	int Area () const
	{
		return Width * Height;
	}
	bool IsSquare() const
	{
		return (Width == Height);
	}
};
int main ()
{
	CPoly * PPoly = new CRect(6, 7);
	...
	CRect* PRect = dynamic_cast<CRect*>(PPoly);
	if(PPRect != 0)
	{
		if(PRect->IsSquare())
			cout << "It's a square" << endl;
		else
			cout << "It's a rectangle" << endl;
	}
	...
	cin.get ();
	return 0;
}
Last edited on
Ohhhhhhhhhhhhhhh So dynamic_cast ensures that the conversion is a valid object. And it is used in abstract classes as well. If it fails it gives you a NULL pointer.
I get it!!!! I got it!!! Thank you Thank You Thank You Thank You Thank You Thank You Thank You
:)
Last edited on
Topic archived. No new replies allowed.