dynamic cast c++

Hi,

im adding a short code. can you please explain me why the following dynamic casting execute what i wrote? i dont understand when this casting fails or not..

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
 
#ifndef MOED_B
#define MOED_B
#include <iostream>
using namespace std;

class Base
{
public:
	Base()	{cout<<"Base: default constructor"<<endl;}
	virtual void f()	{cout<<"Base:f"<<endl;}
	void i()	{cout<<"Base: fun I"<<endl;}
};
class B: public Base
{
public:
	virtual ~B()	{cout<<"B: destructor "<<endl;}
	void f()	{cout<<"B: fun F"<<endl;}
	virtual void h()=0;
	virtual void i()	{cout<<"B: fun I"<<endl;}
	int j()	{cout<<"B: fun J"<<endl;return 2;}
};
class C:public Base
{
public:
	int iN;
	C *c;
	C()	{iN=1;}
	~C()	{cout<<"C: fun F"<<endl;}
	void f()	{cout<<"C: fun F"<<endl;}
	void h()	{cout<<"C: fun I"<<endl;}
	virtual void i()	{cout<<"C: fun I"<<endl;}
	int j()	{cout<<"C: fun J"<<endl;return c->iN;}
};
class D: public C{
public:
	int iN;
	C *c;
	D()	{iN=2;}
	~D()	{cout<<"D: destructor "<<endl;}
	void h()	{cout<<"D: fun H"<<endl;}
	int j()	{cout<<"D: fun J"<<endl;return c->iN;}
};
class E:public B{
public:
	int iN;
	C *c;
	E()	{iN=3;}
	~E()	{cout<<"E:destructor "<<endl;}
	void h()	{cout<<"E: fun H"<<endl;}
	int j()	{cout<<"E: fun J"<<endl;return c->iN;}
	void f()	{cout<<"E: fun F"<<endl;}
};
#endif

---------the main section:----------

D* d0=dynamic_cast<D*>(&c);	// why not??
	D* d1=dynamic_cast<D*>(b);	//why not??
	C* d2=dynamic_cast<D*>(base2);	//why works???
	C* d3=dynamic_cast<C*>(base2);	//why works??


THANKS
What are the types of c, b, base2? Also, remove the ---- from your code and the formatting will be fixed.
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
FULL MAIN:
Base *base1=new E();
	Base *base2=new D();
	B *b= new E();
	base1->f();
	base1->i();
	base2->f();
	base2->i();
	b->f();
	b->i();
	D d;
	d.c=new D();
	d.C::c=new C();
	C c=d;
	cout<<d.j()<<endl;
	cout<<c.j()<<endl;
	cout<<b->j()<<endl;
	D* d0=dynamic_cast<D*>(&c);	// why not??
	D* d1=dynamic_cast<D*>(b);	//why not??
	C* d2=dynamic_cast<D*>(base2);	//why works???
	C* d3=dynamic_cast<C*>(base2);	//why works??
	if(d0)
		d0->f();
	if(d1)
		d1->f();
	if(d2)
		d2->f();
	if(d3)
		d2->f();
	delete base1;
	delete base2;
	delete b;


sorry.. added the main part.
in addition to the dynamic cast, can i have explanation when "delete" is occured and how, in inheritance.
18: c is of type C, and holds only an object of type C, so you can't cast it to a D. What you are doing in line 14 is called "object slicing"; you are throwing away the parts of d that are not in C to create c.

19: b points to an object of type E, which has no relation to D. What did you expect to get here?

20: base2 is pointing to a D, so you can convert to a D with no issues.

21: base2 is pointing to a D, which is inheriting from C, so you put that in a pointer to a C with no issues.
thanks a lot ! so the first casting failed because it is not a pointer?

and 2 more quesions: what is the difference between line 12-13 and how can i access c like that?

and about delete- when deleting a pointer, destructor isnt called, but how it works anyway here (the order of the delete)

thanks again.
???
No, you used the address-of operator so you did have a pointer. The pointer was simply pointing to a C, which cannot be cast to point to a D object since a D object has members and methods that a C doesn't have.

In terms of just accessing the member c, there is no difference. That kind of access (line 13) could be important if you have multiple members defined in different base classes with the same name so you can access each of them.

The desctructor is called when you delete something; that is the point of delete. What are you seeing that makes you think this doesn't happen? I'd also note that you have a memory leak on line 12 and 13, since both objects allocated there are never deleted.
Topic archived. No new replies allowed.