Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss
Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

dynamic_cast

1 view
Skip to first unread message

Mik

unread,
Dec 17, 1999, 3:00:00 AM12/17/99
to
With this folowing code i get the same result. What is the difference.

1:
TControl* provi1=(TControl*)(MDIChildren[f1]->Components[i1]);

2:
TControl *provi1=dynamic_cast<TControl *
>(MDIChildren[f1]->Components[i1]);

Thanks


Sergei Sorokin

unread,
Dec 17, 1999, 3:00:00 AM12/17/99
to
Hi, Mik!

As I understand...

> With this folowing code i get the same result. What is the difference.
>
> 1: TControl* provi1=(TControl*)(MDIChildren[f1]->Components[i1]);
>

In this case you assign an address of MDIChildren[f1]->Components[i1] to a
pointer provi1. It's not checked what type has this component. For this
reason you may have problems.

>
> 2: TControl *provi1=dynamic_cast<TControl *
> >(MDIChildren[f1]->Components[i1]);

When you use dynamic_cast it is checked if at the address of this
component there is an object of requested type. If provi1 is not NULL, it
means, that you really have an object what you ask (TControl in this
case).


--
| ))))) NCC
|
|___
|ooo|
|ooo|
|ooo|
|___|
Sergei.

Jens Hoetger

unread,
Dec 17, 1999, 3:00:00 AM12/17/99
to
On Fri, 17 Dec 1999 11:59:16 +0100, Mik <mfroe...@urbanet.ch> wrote:

>With this folowing code i get the same result. What is the difference.
>
>1:
>TControl* provi1=(TControl*)(MDIChildren[f1]->Components[i1]);

that's old c-style casting. It's bad, ugly and not type-safe. If the
operand is not a TControl* you will get trouble.


>
>2:
> TControl *provi1=dynamic_cast<TControl *
>>(MDIChildren[f1]->Components[i1]);

that's c++like casting using RTTI and the kind you should use. With
dynamic_cast you're casting down in the class hierarchy (from base to
derived). If the operand is not what you're expecting (the operator is
not a derived) dynamic_cast will return 0. Because references can't
get 0-value dynamic_cast will throw an exception bad_cast if it fails.
For other situations there are other c++ casts: static_cast (can use
it with void*), const_cast (cast away const or volatile) or
reinterpret_cast (most dangerous, low-level, bitwise).
--
Jens Hoetger

Harold Howe (TeamB)

unread,
Dec 17, 1999, 3:00:00 AM12/17/99
to

Mik wrote in message <385A1784...@urbanet.ch>...

>With this folowing code i get the same result. What is the difference.
>
>1:
>TControl* provi1=(TControl*)(MDIChildren[f1]->Components[i1]);
>
>2:
> TControl *provi1=dynamic_cast<TControl *
>>(MDIChildren[f1]->Components[i1]);


The second version performs a check at runtime to ensure that he object is
really a TControl. The first version does not do any type checking.

Harold Howe [TeamB]
http://www.bcbdev.com

Alex Vinokur

unread,
Dec 19, 1999, 3:00:00 AM12/19/99
to
In article <385A1784...@urbanet.ch>,

Mik <mfroe...@urbanet.ch> wrote:
> With this folowing code i get the same result. What is the difference.
>
> 1:
> TControl* provi1=(TControl*)(MDIChildren[f1]->Components[i1]);
>
> 2:
> TControl *provi1=dynamic_cast<TControl *
> >(MDIChildren[f1]->Components[i1]);
>
> Thanks
>
>


Here is an example.

Alex

//#########################################################
//------------------- C++ code : BEGIN -------------------

#include <iostream>
#include <string>

//------------------
class AAA
{
public :
virtual ~AAA () {}
};

//------------------
class BBB : public AAA
{
public :
int bbb_;
BBB ()
{
bbb_ = 123;
}
void fooBBB ()
{
cout << "Hi from BBB; bbb_ = "
<< bbb_
<< endl;
}
};

//------------------
class CCC : public AAA
{
public :
string ccc_;
CCC ()
{
ccc_ = "ABCDE";
}
void fooCCC ()
{
cout << "Hi from CCC; ccc_ = "
<< ccc_
<< endl;
}
};


//==================
int main ()
{
AAA* ptrAAA;
BBB* ptrBBB;
CCC* ptrCCC;

//================================
ptrAAA = new BBB;
ptrBBB = dynamic_cast<BBB*> (ptrAAA);
if (ptrBBB == NULL)
{
cout << "ILLEGAL CONVERSION" << endl;
}
else
{
ptrBBB->fooBBB ();
}


//----------------------------------
ptrAAA = new CCC;
ptrBBB = dynamic_cast<BBB*> (ptrAAA);
if (ptrBBB == NULL)
{
cout << "ILLEGAL CONVERSION" << endl;
}
else
{
ptrBBB->fooBBB ();
}


//================================
ptrAAA = new CCC;
ptrCCC = dynamic_cast<CCC*> (ptrAAA);
if (ptrCCC == NULL)
{
cout << "ILLEGAL CONVERSION" << endl;
}
else
{
ptrCCC->fooCCC ();
}


//----------------------------------
ptrAAA = new BBB;
ptrCCC = dynamic_cast<CCC*> (ptrAAA);
if (ptrCCC == NULL)
{
cout << "ILLEGAL CONVERSION" << endl;
}
else
{
ptrCCC->fooCCC ();
}

//================================
//-----------------
ptrAAA = new BBB;
ptrBBB = (BBB*) ptrAAA;
ptrBBB->fooBBB ();

//-----------------
ptrAAA = new CCC;
ptrBBB = (BBB*) ptrAAA;
ptrBBB->fooBBB ();

//-----------------
ptrAAA = new CCC;
ptrCCC = (CCC*) ptrAAA;
ptrCCC->fooCCC ();

//-----------------
ptrAAA = new BBB;
ptrCCC = (CCC*) ptrAAA;
ptrCCC->fooCCC ();

return 0;
}

//------------------- C++ code : END ----------------------

//#########################################################
//------------------- Running Results : BEGIN -------------

Hi from BBB; bbb_ = 123
ILLEGAL CONVERSION
Hi from CCC; ccc_ = ABCDE
ILLEGAL CONVERSION
Hi from BBB; bbb_ = 123
Hi from BBB; bbb_ = 281152 // GARBAGE
Hi from CCC; ccc_ = ABCDE
Bus Error (core dumped)

//------------------- Running Results : END ---------------

//#########################################################
//------------------- Environment -------------------------

g++ -v : gcc version egcs-2.91.57 19980901
(egcs-1.1 release)

uname -sr : SunOS 5.6

//---------------------------------------------------------

//#########################################################

Sent via Deja.com http://www.deja.com/
Before you buy.

0 new messages