advantage of static_cast

Feb 8, 2010 at 4:21am
What is the advantage of static_cast ??
Feb 8, 2010 at 4:33am
Feb 8, 2010 at 7:45am
So it means....static_cast is nothing but a new syntax of old casting stuff !!!!
Feb 8, 2010 at 10:04am
its not that simple buddy..Be carefull with static_cast, as its not type safe.. programmer has to take care while casting.

e.g. static_cast can be used to convert from an int to a char. However, the resulting char may not have enough bits to hold the entire int value.

Be very careful with pointers, use of static_cast which never do a run time check, the errors can be catastrophic..!
use dynamic_cast instead..
Feb 8, 2010 at 1:38pm
Disch's statement sums it up well:


C style casts are probably closer to static_cast.


Having said that, you should not need to use static_cast very often. It, as all casts, are more a necessary evil
than a necessary good.

Feb 8, 2010 at 4:38pm
closed account (1yR4jE8b)
Yeah, casting in general should be avoided whenever possible. They should really be used only when there is no other option.

One of my pet peeves about C++ is that it gives you so much freedom, with so many different casting methods, multiple-inheritance, automatic type conversions, operator overloading etc... But a lot of these things can wreak havoc on debugging if used irresponsibly (which happens more often than not) and can make some programs unmaintainable. It peeves me even more because there are so many better alternatives that are already part of the language.
Feb 9, 2010 at 7:24pm
C style casts are rather more like reinterpret_cast...

Considering adventages of static_cast when compared to C style cast:
- you may not remove constness with static_cast,
- you may not cast completely unrelated types this way (like int* to double*),
- correctly handles multiple inheritance:

1
2
3
4
5
6
7
8
9
10
class A { int a; };
class B { int b; };

class C : A, B {};

void func(B * p)
{
   C * p1 = static_cast<C *>(p): // OK
   C * p2 = (C *)p;              // ough...
}
Feb 9, 2010 at 8:36pm
My earlier comment was taken somewhat out of context.

I later went on to say that C style casts are an unholy mesh of static,reinterpret,const casts. Sometimes you get one, sometimes you get another.

- you may not remove constness with static_cast,


You can't with reinterpret_cast either, can you?

- you may not cast completely unrelated types this way (like int* to double*)


But you can do other static casts, such as int to double.

correctly handles multiple inheritance


I thought it behaved like static_cast in that instance. I came to this assumption because implicit upcasting behaves like static_cast.

I never did actually test it, granted, so I could be wrong. I'd be VERY surprised if it acted like reinterpret_cast though.

I'll test this when I get home.


EDIT:

I was right. C casts behave like static cast when multiple inheritance is involved:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;

class A { int a; };
class B { int b; };

class C : public A, public B { int c; };

int main()
{
    C obj;

    B* b = &obj;
    A* a = &obj;

    //=============
    cout << "Casting from A:\n";
    cout << "reinterpret:  " << reinterpret_cast<C*>(a) << "\n";
    cout << "static:       " <<      static_cast<C*>(a) << "\n";
    cout << "C style:      " <<                 (C*)(a) << "\n";
    cout << "\n\n";

    //=============
    cout << "Casting from B:\n";
    cout << "reinterpret:  " << reinterpret_cast<C*>(b) << "\n";
    cout << "static:       " <<      static_cast<C*>(b) << "\n";
    cout << "C style:      " <<                 (C*)(b) << "\n";
    cout << "\n\n";

    char c;
    cin >> c;
    return 0;
}
Casting from A:
reinterpret:  0xbfd7585c
static:       0xbfd7585c
C style:      0xbfd7585c


Casting from B:
reinterpret:  0xbfd75860
static:       0xbfd7585c
C style:      0xbfd7585c



So to recap... C casts are just like static_cast except for the following:

- can cast between pointers of unrelated types (here, C casts behave like reinterpret_cast)
- can cast away const/volatile modifiers (here, C casts behave like const_cast)
Last edited on Feb 10, 2010 at 3:36am
Feb 10, 2010 at 7:26am
This is quite weird. C++ never stops to suprise me.

BTW, using static cast to perform conversions like int to double is not necessary, as these types are implicitly convertible. I think the only real application of static cast is to convert base type pointers/references to derived type pointers/references.
Last edited on Feb 10, 2010 at 7:27am
Topic archived. No new replies allowed.