By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,805 Members | 1,645 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,805 IT Pros & Developers. It's quick & easy.

Should I be using dynamic_cast or c-style casting here...

P: n/a
I'm coding in MC++ and I'm using the System::Collections data structures to
store my own objects. When I get something out of a hashmap, should I be
using dynamic_cast or old C-style casting? In other words, should it be

Namespace::ObjectName* someObject =
(Namespace::ObjectName*)hashMap->Item(key);

or

Namespace::ObjectName* someObject =
dynamic_cast<Namespace::ObjectName*>(hashMap->Item(key));


Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
0to60 wrote:
I'm coding in MC++ and I'm using the System::Collections data
structures to store my own objects. When I get something out of a
hashmap, should I be using dynamic_cast or old C-style casting? In
other words, should it be

Namespace::ObjectName* someObject =
(Namespace::ObjectName*)hashMap->Item(key);

or

Namespace::ObjectName* someObject =
dynamic_cast<Namespace::ObjectName*>(hashMap->Item(key));


Depends whether you want safety or speed. If you "know" what kind of object
it is, go ahead and use the C-style cast (or use static_cast). If there's a
chance that some other kind of object is present, use dynamic_cast.

A common idiom is to use something like this:

assert(dynamic_cast<NameSpace:::ObjectName*>(hashM ap->Item(key));

Namespace::ObjectName* someObject =
static_cast<Namespace::ObjectName*>(hashMap->Item(key));

-cd
Nov 16 '05 #2

P: n/a
C-style cast on managed types in MC++ translates into a __try_cast, so it is
equally as safe as dynamic_cast.

Ronald Laeremans
Visual C++ team

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:eY**************@TK2MSFTNGP12.phx.gbl...
0to60 wrote:
I'm coding in MC++ and I'm using the System::Collections data
structures to store my own objects. When I get something out of a
hashmap, should I be using dynamic_cast or old C-style casting? In
other words, should it be

Namespace::ObjectName* someObject =
(Namespace::ObjectName*)hashMap->Item(key);

or

Namespace::ObjectName* someObject =
dynamic_cast<Namespace::ObjectName*>(hashMap->Item(key));
Depends whether you want safety or speed. If you "know" what kind of

object it is, go ahead and use the C-style cast (or use static_cast). If there's a chance that some other kind of object is present, use dynamic_cast.

A common idiom is to use something like this:

assert(dynamic_cast<NameSpace:::ObjectName*>(hashM ap->Item(key));

Namespace::ObjectName* someObject =
static_cast<Namespace::ObjectName*>(hashMap->Item(key));

-cd

Nov 16 '05 #3

P: n/a
"Ronald Laeremans [MSFT]" <ro*****@online.microsoft.com> wrote in message
news:O1**************@TK2MSFTNGP11.phx.gbl...
C-style cast on managed types in MC++ translates into a __try_cast, so it is equally as safe as dynamic_cast.


Interesting. How about static_cast<>? Does that translate into a
__try_cast also?
Nov 16 '05 #4

P: n/a
No, it does not.

Ronald

"0to60" <ho****************@yahoo.com> wrote in message
news:IW*****************@newssvr33.news.prodigy.co m...
"Ronald Laeremans [MSFT]" <ro*****@online.microsoft.com> wrote in message
news:O1**************@TK2MSFTNGP11.phx.gbl...
C-style cast on managed types in MC++ translates into a __try_cast, so
it is
equally as safe as dynamic_cast.


Interesting. How about static_cast<>? Does that translate into a
__try_cast also?

Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.