cast in C++

B

Bob Keith

Hello,

I am studying some source code, on which I will do some further
development.

I couldn't understand the following code very well.

*****

stateset_result *r=(stateset_result*)p[1]->compute;

*****

I think the author use "cast" here, but I don't understand very well
of the concept of cast, and how to use them.

Could you explain it to me using the above code as an example. Many
thanks.
 
V

Victor Bazarov

Bob Keith said:
I am studying some source code, on which I will do some further
development.

I couldn't understand the following code very well.

*****

stateset_result *r=(stateset_result*)p[1]->compute;

*****

I think the author use "cast" here, but I don't understand very well
of the concept of cast, and how to use them.

C++ has good and relatively solid rules for conversions from types
to other types. Casts are applied when such conversions don't work
or the compiler can't figure out which conversion to use.

There are three kinds of casts in C++. Explicit casts: static_cast,
const_cast, dynamic_cast, reinterpret_cast. Read about those in
your favourite C++ book. Function-style casts: type(variable). And
C-style casts (type)variable. The cast in the example you gave is
apparently C-style. The latter of three types of casts is the most
dangerous and least recommended. C++ code can be easily written
without C-style casts.

In the light of this, it is impossible to conclude anything from
the given fragment (one statement is really not enough), as to why
the cast is there and what it helps to accomplish. Perhaps if you
post more code...

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top