[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-core-devel
Subject: Re: KPresenter
From: Lubos Lunak <l.lunak () sh ! cvut ! cz>
Date: 1999-11-01 22:41:39
[Download RAW message or body]
On Po, 01 lis 1999, Kurt Granroth wrote :
>Reginald Stadlbauer wrote:
>> I mean, you can always live without C++ casts, because
>>
>> dynamic_cast<AClass>( aPtr )->doSomething()
>>
>> can be replaced by
>>
>> ( (AClass*)aPtr )->soSomething().
>>
>> Itīs just that the first (C++) solution is the better one.
>
>Why is it better? I'll admit that I've never used dynamic_cast before
>as I couldn't see the point.
The difference between dynamic_cast<> and the C cast is that dynamic_cast<>
dynamically ( hence the name ) checks if the conversion can be actually done.
The C lets you convert anything to anything else, but when using
dynamic_cast<>, if the instance is not of the requested type ( or derived from
it ), dynamic_cast<> will refuse to perform the conversion and returns NULL ( or
throws an exception ). You can never do this with the plain C typecasting.
In the example above, the usage of dynamic_cast<> is a bit strange. If aPtr
doesn't point to a AClass type instance ( or derived ), it returns NULL and
doSomething() will get called with this == NULL. Either you're sure
dynamic_cast<> will always succeed and you don't need to use it, as it's
not a trivial operation ( use static_cast<> instead ), or you need dynamic
checking, and you also have to check for the case when it fails.
Lubos Lunak
l.lunak@email.cz http://dforce.sh.cvut.cz/~seli
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic