473,395 Members | 2,689 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,395 software developers and data experts.

Why casting necessary?

alg
Why is so necessary sometimes in C++ that you have to cast a pointer between
a base object and a derived object? Could you give me some examples for me
to understand this concept deeply?

Thanks for your help!
Jul 19 '05 #1
2 1843
"alg" <al******@yahoo.com> wrote:
Why is so necessary sometimes in C++ that you have to cast a pointer between
a base object and a derived object? Could you give me some examples for me
to understand this concept deeply?

Thanks for your help!


Here is an example.

<http://www.objectmentor.com/resources/articles/visitor>

The article above discribes the Acyclic Visitor pattern. The ability to
cast a base object to it's specialized derived type is necessary for
this pattern.
Jul 19 '05 #2
John Dibling <dib@substitute_my_full_last_name_here.com> wrote in message
news:b1********************************@4ax.com...
On Mon, 14 Jul 2003 11:49:16 -0700, "Mike Wahler"
<mk******@mkwahler.net> wrote:
alg <al******@yahoo.com> wrote in message
news:mz*********************@bgtnsc05-news.ops.worldnet.att.net...
Why is so necessary sometimes in C++ that you have to cast a pointerbetween
a base object and a derived object?


Usually, it's because of a poor design.


One cannot make this claim categorically.


"Usually" isn't categorical.
It is often necesarry and
proper to cast, although I would agree that casts are easy to use
inappropriately. In COM programming for example, a very common
scenario is to have many objects which are similar in nature, but have
some unique prepoerties. For example, suppose you are writing stock &
option trading software. Your software might abstract common elements
in to an interface called IOrder, where attirbutes specific to stock
trades (such as ticker symbol) are in the derived interface
IStockOrder, and option-specific attributes (such as OPRA code and
expiration date) are in the derived interface IOptionOrder. Your
system would likely need to make available to clients an array of
"all" orders, in which case the array would be an array of IOrder*'s.
If you subscribe to the theory that casts are categorically wrong,
"Usually" isn't categorical.
then your only other option is to have one array for IStockOrder*'s
and another for IOptionOrder*'s. If this were the case, you must have
duplication of at least some code, which I would argue is much more
indicitave of a design flaw.


If I had to use members that belonged only to IStockOrder or IOptionOrder,
then I would probably have one collection of IStockOrder*, another of
IOptionOrder*, and perhaps another of the two mixed up in a collection of
IOrder*. I would use each collection as appropriate. The code would be
cast-free and would execute more efficiently. Ideally, everything would be
done polymorphically through a single collection of IOrder*, but if it can't
be then why use a collection of IOrder* if you are going to have to downcast
them all later?

One potential big problem with downcasts is where the project is large and
you have to add another type, which might force you to wade through all your
code, find all your downcasts, and possibly add another case for the added
type all over the place.

I don't see where the code duplication is in having separate collectoins.
You are doing different things with each collection. That's why they are
separate.

DW

Jul 19 '05 #3

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

Similar topics

5
by: Suzanne Vogel | last post by:
** Isn't the 'static_cast' operator the same as traditional type casting? ie, Aren't the following ways of getting b1, b2 the same? // 'Derived' is derived from 'Base' Derived* d = new...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
33
by: hermit_crab67 | last post by:
Can someone explain to a C newbie why this doesn't work as I expect it to work? (expectations clearly outlined in the printf statement in main routine) OS: Linux 2.4.26 GCC: 2.95.4 void...
14
by: mr_semantics | last post by:
I have been reading about the practise of casting values to unsigned char while using the <ctype.h> functions. For example, c = toupper ((unsigned char) c); Now I understand that the standard...
7
by: Jim Bancroft | last post by:
Hi everyone, A basic one here, I think. I haven't found the pattern yet, but sometimes when I cast a variable to another type using the "C" style cast operator the compiler refuses to play...
8
by: David Williams | last post by:
Hi all, I have a templated Vector3D class which holds (x,y,z) components as the specified type. I quite often wish to cast a Vector3D holding ints into a Vector3D holding floats and vice versa....
5
by: brekehan | last post by:
I've always been a little sketchy on the differences between static, dynamic, and reinterpret casting. I am looking to clean up the following block by using C++ casting instead of the C style...
12
by: Taras_96 | last post by:
Hi everyone, Herb Schildt in "C++, the complete reference" writes: "In C++, it is illegal to convert one type of pointer into another without the use of an explicit type cast. For this...
32
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION"...
101
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
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...

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.