473,857 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

I'm coding in MC++ and I'm using the System::Collect ions 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::Obje ctName* someObject =
(Namespace::Obj ectName*)hashMa p->Item(key);

or

Namespace::Obje ctName* someObject =
dynamic_cast<Na mespace::Object Name*>(hashMap->Item(key));


Nov 16 '05 #1
4 1890
0to60 wrote:
I'm coding in MC++ and I'm using the System::Collect ions 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::Obje ctName* someObject =
(Namespace::Obj ectName*)hashMa p->Item(key);

or

Namespace::Obje ctName* someObject =
dynamic_cast<Na mespace::Object Name*>(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*>( hashMap->Item(key));

Namespace::Obje ctName* someObject =
static_cast<Nam espace::ObjectN ame*>(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.nos pam>
wrote in message news:eY******** ******@TK2MSFTN GP12.phx.gbl...
0to60 wrote:
I'm coding in MC++ and I'm using the System::Collect ions 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::Obje ctName* someObject =
(Namespace::Obj ectName*)hashMa p->Item(key);

or

Namespace::Obje ctName* someObject =
dynamic_cast<Na mespace::Object Name*>(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*>( hashMap->Item(key));

Namespace::Obje ctName* someObject =
static_cast<Nam espace::ObjectN ame*>(hashMap->Item(key));

-cd

Nov 16 '05 #3
"Ronald Laeremans [MSFT]" <ro*****@online .microsoft.com> wrote in message
news:O1******** ******@TK2MSFTN GP11.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******** *********@newss vr33.news.prodi gy.com...
"Ronald Laeremans [MSFT]" <ro*****@online .microsoft.com> wrote in message
news:O1******** ******@TK2MSFTN GP11.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
2682
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 option. Let's call the id of a class "cid" (for "class id"). The function signature should look like this: ******************************************
8
2606
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 it is the case in the following example: ----------------------------------------------------------- #include <iostream>
1
1536
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 I assume the result is consistente with the Standard? Tue May 10 21:24:24:> cat foo.cc #include <iostream> struct Foo{virtual ~Foo(){} }; struct Bar{virtual ~Bar(){} }; int main(){
3
2541
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 that this doesnt work if the destructor is not virtual or when the class has no virtual members. I would like to know is there anything wrong in what I am thinking. (I agree it is better to keep base class dtor as virtual, but supposing it is not...
19
4079
by: tthunder | last post by:
Hi @all, I've got an interesting problem. These are my classes: ---------------------- class fooBase {
2
1760
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 (Subject is an object with protected constructor) The problem is, whenever I used dynamic_cast to cast a Subject pointer to XXX (I am quite sure Subject pointer is actually pointing to XXX), a visual c++ runtime error will occur.
5
2042
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). You can find a problem there, which cannot be solved by dynamic_cast, because the pointers can be NULL, and I only want to know, if the pointer type is derived from another pointer type. BTW: I cannot create a temporary instance, because used...
33
3588
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 }; Type GetType() { return type_; }
15
12328
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? Thanks in advance! Regards! Bo
25
3146
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 ``GrandChild'', i.e. types not upper than Child in the above class hierarchy, dynamic_cast<Child*>pBase
0
9923
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11082
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10711
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10807
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10394
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7939
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5774
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4190
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.