C++ 11 feature: static_cast

Casting is a new feature in C++. Below are the different types of csating available:

1. Static Cast
2. Dynamic Cast
3. Const Cast
4. Reinterpret Cast

static_cast in smart pointers:

Points on Static cast:

1. static_cast is used for compile time checking.
2. static_cast can easily be searched in C++, thus making easily readable code.
3. static_cast is safe.

static_cast operator syntax:

static_cast<type> (expression);

Now you might think, why to use static_cast, when you can do C-Style casting?

Consider the below example of C-Style:

char c = 'a';
int *p = (int*) c;

Now the statement “int *p = (int*) c;” will pass at compile time but will crash at run time.

But when you use “static_cast” as below:

int *q = static_cast<int *> (&c);

It will give error at compile time only.

“static_cast” should be used in implicit conversion.

You should not be used for inheritance based conversion. “dynamic_cast” should be used.

We shall see about dynamic_cast in later chapters.

Let us understand “static_cast” with help of numerious example:

Syntax of static_cast cast:

data_type *var_name = static_cast <data_type *>(pointer_variable);

Example 1: Simple program.

#include <iostream>
#include <set>
//for more C++ tutorial visit www.ProDeveloperTutorial.com
using namespace std;

int main() 
{ 
    float fnum = 10.23;
    
    int inum = fnum;
    
    int b = static_cast<int> (fnum);
    
    cout<<"Type of fnum is "<<typeid(fnum).name()<<endl;
    cout<<"Type of inum is "<<typeid(inum).name()<<endl;
    cout<<"Type of inum is "<<typeid(b).name()<<endl;

    return 0; 
}

Output:

Type of fnum is f
Type of inum is i
Type of inum is i

Example 2: Simple program for compile time checking

#include <iostream>
#include <set>
//for more C++ tutorial visit www.ProDeveloperTutorial.com
using namespace std;

int main() 
{ 
    char c = 'a';
    
    int *ptr = (int*) &c;
    
    int *ptr_1 = static_cast<int*> (&c);

    return 0; 
}

Error:

Main.cpp:12:18: error: static_cast from 'char *' to 'int *' is not allowed
    int *ptr_1 = static_cast<int*> (&c);
                 ^~~~~~~~~~~~~~~~~~~~~~
1 error generated.

As you can see fromt he output, the error did not produce on the line “int *ptr = (int*) &c;” but on the line “int *ptr_1 = static_cast<int*> (&c);”. This is the advantge of static_cast.

Example 3: Simple program for inheritance

#include <iostream>
#include <set>
//for more C++ tutorial visit www.ProDeveloperTutorial.com
using namespace std;

class Base
{
    
};

class Derived : public Base
{
    
};

class OtherClass
{
    
};


int main() 
{ 
    Derived *d = new Derived;
    
    Base *b = static_cast<Base*> (d);//works

    OtherClass *o = static_cast<OtherClass*> (d);//Error 

    return 0; 
}

Error:

Main.cpp:28:21: error: static_cast from 'Derived *' to 'OtherClass *', which are not related by inheritance, is not allowed
    OtherClass *o = static_cast<OtherClass*> (d);//Error 
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *