473,797 Members | 2,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static_cast vs reinterpert_cas t

Hi,

I have a
class A : public B {...member functions...... data members};

and am doing the following
A *p=new A();
void *p=static_cast< void *>(p);
factory_instanc e->process(p);

Here p is passed to a function, which accepts void ptr. That function
need to cast it back
A *pp=static_cast <A *>(p);

The function is in the factory which accepts void *p only, the specific
implementations need to cast the pointer back to the expected class
and use it.

Question:Though both works fine, yet I want to know what is more
appropriate in this situation static_cast OR reinterpert_cas t

The books suggests
static_cast= "For "well-behaved" and "reasonably
well-behaved" casts,including things you might now do without a cast
reinterpret_cas t=To cast to a completely different meaning. The key
is that you'll need to cast back to the original type to use it
safely.

But I am not able to interpret the sentences in this context :-)

Jul 10 '06
24 4769
Frederick Gotham wrote:
Rolf Magnus posted:

>size_t is unsigned and can't have negative values. size_t(-1) tells the
compiler that you want -1 converted to size_t, IOW, it's a cast.


There's an implicit conversion from int to size_t, so no cast is required.
Well, it's there, no matter if it's required or not.
When dealing with intrinsic types, the following are exactly equivalent:

short(5)

(short)5
Yes. Both are casts.
I myself prefer the latter, for two reasons:

(1) You can have types which consist of more than one word, e.g.
"unsigned short".
That's a good point.
(2) It resembles a cast.
Well, it is a cast, just like the first one.
I dislike the former because:

(1) It looks like the construction of a user-defined class type.
That's one reason why I like it. It uses built-in types more consistently
with classes by using the same syntax.
Btw: What do you do in a template, where the type might be either one?
(2) It doesn't resemble a cast at all.
Well, if you are used to the latter, because you know it from C, then you
might think like that. But basically, none of the two looks more "castish"
than the other.
(3) You can't use types which consist of more than one word.
Yes, that's a restriction that will hopefully be fixed some time.
One thing I like about it is that the value is in parenthesis, so in a
longer exression, it's easier to see which part of it is cast.
Jul 13 '06 #21
In message <xQ************ *******@news.in digo.ie>, Frederick Gotham
<fg*******@SPAM .comwrites
>Richard Herring posted:
>> for(size_t i = len - 1; size_t(-1) != i; --i)

I'd lose the -1 altogether ;-)

for (size_t i = len; i-- != 0; )


Yes but this has re-arranged the natural order of the loop.

I like my "for" loops to have four phases:

(1) The "initialisa tion stuff" is executed first of all.

(2) The condition is tested.

(3) The body is executed.

(4) The "prepare for next iteration" stuff is executed.
Your example mixes (2) and (4), which I don't like.
It puts (4) before (3), that's all. It's just another idiom, and a very
clear one once you're used to it. The only numbers that appear in it are
0 and len, exactly the same as for the forward loop. Other solutions all
require an off-by-one representation of the index and either mix signed
and unsigned, as with the cast above, or do something even more
contorted.

--
Richard Herring
Jul 13 '06 #22
Richard Herring posted:

>>Your example mixes (2) and (4), which I don't like.
It puts (4) before (3), that's all. It's just another idiom, and a very
clear one once you're used to it. The only numbers that appear in it are
0 and len, exactly the same as for the forward loop. Other solutions all
require an off-by-one representation of the index and either mix signed
and unsigned, as with the cast above, or do something even more
contorted.

Let's put it down to a difference in taste : ).
--

Frederick Gotham
Jul 13 '06 #23
Jim Langston wrote:
"Ron Natalie" <ro*@spamcop.ne twrote in message
news:44******** *************** @news.newshosti ng.com...
>Rolf Magnus wrote:
>>I tend to use constructor-style casts in such a situation:

for(size_t i = len - 1; size_t(-1) != i; --i)

But I think that's just because C style casts look so old-style to me
now ;-)
But a single arg function-style cast is identical in meaning to the
C-style cast. You're back to it possibly meaning static, const,
reinterpret or combinations of the above (as well as busting access
control which there is no C++-style equivelent to).

Actually, not at all. size_t(-1) tells me exactly what is going on. This
is a size_t variable that is being initialized to -1. There is no ambiguity
at all. It is not techinically a cast, I don't think.

It's is exactly the same as the c-style cast.
The definition of the "explicit type conversion (functional notation)"
says:

If the expression list is a single expression, the type conversion
expression is equivalent (in definedness, and if defined in meaning) to
the corresponding cast expression (5.4).
Jul 13 '06 #24
Frederick Gotham wrote:
When dealing with intrinsic types, the following are exactly equivalent:

short(5)

(short)5
They are exactly equivelent no matter WHAT type is being used.
>
Jul 13 '06 #25

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

Similar topics

7
6367
by: Gary Labowitz | last post by:
Am I doing this correctly? It is a sample program for my class. #include <iostream> using namespace std; int main( ) { int x=3, y=4;
11
5155
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 one type to another. However, I'm certain that there is something else that is happening. Can someone explain the difference or recommend an online site that can explain it to me?
9
4175
by: news.ir.com.au | last post by:
Hi, In the following code I get the compiler error: error C2243: 'static_cast' : conversion from 'class B *' to 'class A *' exists, but is inaccessible I understand why I get this error and can usually get around the situation by inserting a "using A::..." statement inside class B, however, due to this being a static cast, what is the syntax?
26
2559
by: Steven T. Hatton | last post by:
The code shown below is an example from the Coin3D documentation. I believe the use of the C-style cast is safe under the circumstances, but from what I've been exposed to (TC++PL(SE)), I would favor using a static_cast. Is there any technical reason to favor the C-style over a static_cast? http://doc.coin3d.org/Coin/index.html void foo(SoNode * node) { if (node->getTypeId() == SoFile::getClassTypeId()) {
3
1930
by: shrishjain | last post by:
Hi All, Do people frequently use static_cast, const_cast etc in industry?.. I only saw them in books, and never in real code.. Shrish
2
1961
by: Amit | last post by:
Greetings. I am having some problem while using a cast operation(static_cast and/or dynamic_cast) between base and derived objects when passing to functions. what I have is something like this.. template <class T> class Node{ protected: T e; Node* left;
19
3764
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 other.
5
3915
by: jason.cipriani | last post by:
There have been some recent threads about casting pointers to and from void* that have me rethinking some of my usual practices. I have a couple of questions. 1. What is the purpose of C++'s static_cast<>? In other words, is there any real difference between statements like (with non-pointer types): double a = 3.4; int b = (int)a; // <--- this
3
367
by: Rahul | last post by:
Hi, Everywhere I read that static_cast<only work fine for the conversion which are implicitly allowed by the compiler hence the following does not work int *i; double *d; d = i; // Compilation error , OK i= static_cast<int *>(d); // Compilation error. OK
0
9685
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
10468
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
10021
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...
0
9063
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7559
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
6802
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5458
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...
2
3748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.