473,386 Members | 1,679 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,386 software developers and data experts.

static_cast<>

alg
I read somewhere that static_cast<> is not safe, sometimes fatal. Could
someone tell me why it is still around and when it should be used in
general? When will it be unsafe to use it?

Thanks for your help!
Jul 19 '05 #1
2 2613
alg <al******@yahoo.com> wrote in message
news:NF*********************@bgtnsc04-news.ops.worldnet.att.net...
I read somewhere that static_cast<> is not safe, sometimes fatal. Could
someone tell me why it is still around and when it should be used in
general?
An example of a safe usage is downcasting a base-class pointer to a
derived-class pointer when you absolutely _know_ that the pointer will
really point to an instance of the derived class after the cast. You might
choose static_cast over dynamic_cast in such as case for greater execution
speed. (Whether you could have avoided the downcast altogether is another
matter. Downcasts are distasteful.)
When will it be unsafe to use it?


When there is no sensible conversion to the destination type. If you have a
Shape* that you think points to what was created as a Rectangle, then it
would be unsafe to static_cast it to an Ellipse*, for example. Since
programmers generally don't do such things deliberately, unsafe usage would
nearly always result from the programmer's misunderstanding of what the
object can be safely converted to. In this respect static_cast is unsafe
because the programmer is saying to the compiler, "trust me", but now and
then that trust is misplaced, and a bug or potential bug is the result.

DW

Jul 19 '05 #2
alg
David White <no@email.provided> wrote in message
news:mx****************@nasal.pacific.net.au...
alg <al******@yahoo.com> wrote in message
news:NF*********************@bgtnsc04-news.ops.worldnet.att.net...
I read somewhere that static_cast<> is not safe, sometimes fatal. Could
someone tell me why it is still around and when it should be used in
general?


An example of a safe usage is downcasting a base-class pointer to a
derived-class pointer when you absolutely _know_ that the pointer will
really point to an instance of the derived class after the cast. You might
choose static_cast over dynamic_cast in such as case for greater execution
speed. (Whether you could have avoided the downcast altogether is another
matter. Downcasts are distasteful.)

Thanks, David!

How do "you absolutely _know_ that the pointer will really point to an
instance of the derived class after the cast"? By checking the return value
to see if it's NULL or not?


Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Wenjie | last post by:
Hello, I have a class B to mediate/isolate between implementation and their users (like used in an abstract factory class pattern): // B.h class B { static B* getImpl(); static void...
3
by: alg | last post by:
dynamic_cast<> comes in play when to perform conversion from a pointer to a base class to a pointer to a derived class. I don't understand: 1. why this is so necessary since we can either use...
11
by: Scott Brady Drummonds | last post by:
Hi, everyone, I've checked a couple of on-line resources and am unable to determine how reinterpret_cast<> is different from static_cast<>. They both seem to perform a compile-time casting of...
6
by: bonham | last post by:
i m a beginner in C++. I have some questions: what is static_cast<unsigned>? i saw someone use static_cast<unsigned> and static_cast<unsigned char> together. why? e.g....
24
by: Marcus Kwok | last post by:
Hello, I am working on cleaning up some code that I inherited and was wondering if there is anything wrong with my function. I am fairly proficient in standard C++ but I am pretty new to the .NET...
19
by: PengYu.UT | last post by:
I see some code use static_cast<some_pointer_type>(0) instead of NULL to describe null pointer. I'm wondering what is the pros and cons of each way. Is there any reason why we should one verses the...
3
by: Nate Barney | last post by:
I have: // base class for Vector and Matrix template <unsigned N,typename Value=float> class NonScalar { public: Value *ptr() { return e; } const Value *ptr() const { return e; }
11
by: Bo Peng | last post by:
Dear C++ experts, I need to store and retrieve a meta information that can be int or double. The program would be significantly simpler if I can handle two types uniformly. Right now, I am using...
15
by: Grizlyk | last post by:
Hello. Returning to question of manual class type identification, tell me, for ordinary inheritance is C++ garantee that dynamic_cast<Derived*>(Base*) can be implemented similarly to ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.