beginner's question on static_cast and const_cast

S

subramanian100in

when are the use of static_cast and const_cast essential ?
Stroustrup, in his book TC++PL(3rd edition) in page number 139
(in Section 6.5 Advice - [4]), has advised
"Avoid explicit type conversion(casts)"

Given this, why do we have static_cast and const_cast
provided by the language ?

Kindly explain

Thanks
V.Subramanian
 
J

Jim Langston

when are the use of static_cast and const_cast essential ?
Stroustrup, in his book TC++PL(3rd edition) in page number 139
(in Section 6.5 Advice - [4]), has advised
"Avoid explicit type conversion(casts)"

Given this, why do we have static_cast and const_cast
provided by the language ?

I use a library written by someone else (I don't have the code to it, just
an interface). Originally he did not write the function calls constant
correct, so would have things like:

void drawtext( char* Text, int x, int y );

Now, I'm using std::strings To use this function with my std::strings poses
a problem, as a string.c_str() returns a const char*, and you can not call a
function expecting a non constant pointer with a constant pointer. I
verified with the developer that the function did not change the string, and
so I could do this:

drawtext( const_cast<const char*>( MyString.c_str() ), x, y )

a bit ugly but it does the trick. It throws away the constantness. Usually
this would be used when interfacing with C functions or C++ functions that
are not constant correct. This would do the same thing the C style cast of:

drawtext( (const char*)MyString.c_str(), x, y )
would do. But one of the advantages of making so verbose is we want to get
rid of them. I talked to the developer and he went through and made the
functions constant correct which allowed me to use:

drawtext( MyString.c_str(), x, y )

There are reasons for static cast also, sometimes to just make the compiler
shut up about warnings without having to disable them, sometimes to cast
variables explicitly.

static_cast is not really as dangerous as reinterpret_cast though. Unless
you know what you are doing and think about all the consenquences
reinterpret_cast can get you in deep water. I find it useful, however, when
extracting binary data from some data.
 
S

Stuart Redmann

when are the use of static_cast and const_cast essential ?
Stroustrup, in his book TC++PL(3rd edition) in page number 139
(in Section 6.5 Advice - [4]), has advised
"Avoid explicit type conversion(casts)"

He is right. Avoid them if _possible_.
Given this, why do we have static_cast and const_cast
provided by the language ?

Sometimes casts are just _necessary_. When you have to use one, you have to
decide which casting mechanism is appropriate. Const_cast should only be used to
"cast away" constness. I have only ever seen const_casts in relation to
non-const correct APIs.

Static_cast is necessary when you want to down-cast a pointer of some base class
into a pointer of a derived class. Static_cast does not make any checks as
dynamic_cast does. Static_cast if often used when either run-time type
information are not available for the code or when you want to avoid the
overhead of a dynamic_cast.

As general rule of thumb one can say that the need for dynamic_casts means that
your design is broken. I only use dynamic_casts in conjunction with Microsoft's
MFC library (which suffers from numerous design flaws, but it was engineered
quite a long time ago). If you use templates excessively (especially the
Curiously Recurring Template pattern), you'll come to the point where you have
to use static_casts quite often.

Regards,
Stuart
 
J

Juha Nieminen

Stuart said:
Static_cast is necessary when you want to down-cast a pointer of some
base class into a pointer of a derived class.

That's not the only use for static_cast. It can also be legitimately
used in things like: int i = static_cast<int>(aDouble);
 
R

red floyd

Stuart said:
Static_cast is necessary when you want to down-cast a pointer of some
base class into a pointer of a derived class. Static_cast does not make
any checks as dynamic_cast does. Static_cast if often used when either
run-time type information are not available for the code or when you
want to avoid the overhead of a dynamic_cast.

Also for round-tripping to void*:

T* pT;
// ...
void* pVoid = pT;
// ...
pT = static_cast<T*>(pVoid);

Often used with APIs that require a C callback.
 

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

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top