473,409 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

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

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
4 1873
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Suzanne Vogel | last post by:
Hi, I've been trying to write a function to test whether one class is derived from another class. I am given only id's of the two classes. Therefore, direct use of template methods is not an...
8
by: Thomas Lorenz | last post by:
Hello, first, I didn't find any reference to my question through googling. dynamic_cast uses RTTI to determine if the cast expression is valid. Invalid casts of pointers give a '0'-result. As...
1
by: Steven T. Hatton | last post by:
The result shown below doesn't surprise me now. But it did several months ago when I followed some bad advice and tried to check if I had a live object at the address referenced by a pointer. Can...
3
by: Ganesh | last post by:
On devx site, I saw following code. It says when a derived class is tried to cast to base type, it looks at the missing vtable and complains if the object is already deleted. I am of the opinion...
19
by: tthunder | last post by:
Hi @all, I've got an interesting problem. These are my classes: ---------------------- class fooBase {
2
by: yccheok | last post by:
Hello, I have an object called XXX previously derived from CDocument in my MDI project. Later, I create an concrete class called Subject. And I let XXX to have multiple inheritance from Subject...
5
by: tthunder | last post by:
Hi @all, Perhaps some of you know my problem, but I had to start a new thread. The old one started to become very very confusing. Here clean code (which compiles well with my BCB 6.0 compiler)....
33
by: mscava | last post by:
Well I've got a problem, that is more theoretical than practital. I need to know benefits of RTTI. I see another way of doing it... class A { public: ~virtual A() {} enum Type { X, Y, Z }; ...
15
by: Bo Yang | last post by:
Hi, I can understand static_cast, reinterpret_cast and const_cast, they all work at compile time. But I can figure out how the C++'s dynamic- cast works? Could you please explain how for me?...
25
by: lovecreatesbea... | last post by:
Suppose I have the following three classes, GrandBase <-- Base <-- Child <-- GrandChild The following cast expression holds true only if pBase points object of type of ``Child'' or...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.