Skip to content

Commit

Permalink
69 - Casting
Browse files Browse the repository at this point in the history
  • Loading branch information
Dalton Lima committed Jun 13, 2019
1 parent 70379c5 commit 8422cb5
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
92 changes: 92 additions & 0 deletions 069-Casting/Casting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Casting in C++

C++ is a **strongly typed language**, that evolved from C language, for that matter we could use **C style casting** as well as **C++ style casting** in C++ as expected.

One related topic to casting is ```implicit conversions```, which is a lossly cast, and so C++ (the compiler) knows how to convert the involved types, e.g. assigning an ```int``` to a ```double```.

```cpp
int a = 3;
double b = a;
```

Another topic is ```explicit conversions```, that we can see on the example bellow, where we jave this ```(int)``` (that is optional), but if we supply it, we are explicitly casting. This is a **C style casting**.

```cpp
double c = 3.14;
int d = (int)c;
```

## C++ style cast

There are four types of casts in C++. Technically, we can't do anything that we can't do with C style cast. What C++ style cast offers is more a **synthatic suggar** and some compilation-time checkings.
One exception is ```dynamic_cast``` that can return ```null``` in some cases.

A benefit of the C++ style casting is that they are **searchable**, and thus we can be more clear about our intents.

* ```static_cast```
* ```reinterpret_cast``` // Very related to [type punning](../066-TypePunning/TypePunning.md)
* ```dynamic_cast```
* ```const_cast```

```const_cast``` is basically used to add or remove ```const``` to something. You can add ```const``` implicitly either way, but in order to remove ```const``` we need to use ```const_cast```.

```cpp
double c = 3.14;
int d = static_cast<int>c;
```

```cpp
double c = 3.14;

// This is produce a compilation error
double d = reinterpret_cast<AnotherClass*>(&c);

// This will not produce a compilation error
double e = reinterpret_cast<AnotherClass*>(&c);
```

## Another example

```cpp
#include <iostream>

class Base
{
public:
Base() { }
virtual ~Base() { }
};

class Derived : public Base
{
public:
Derived() { }
~Derived() { }
};

class AnotherDerivedClass : public Base
{
public:
AnotherDerivedClass() { }
~AnotherDerivedClass() { }
}

int main()
{
Derived* derived = new Derived();

Base* base = derived;

//AnotherDerivedClass* another = static_cast<AnotherDerivedClass*>(base);

// This cast CAN return null, if the intended cast isn't possible.
AnotherDerivedClass* another = dynamic_cast<AnotherDerivedClass*>(base);

if (another != null)
{
// Do stuff with another
}
}
```
Related topic: Run Time Type Information (RTTI)
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ These are my personal notes and codes from an awesome [C++ Series by TheCherno](
* [Type Punning](066-TypePunning/TypePunning.md)
* [Unions](067-Unions/Unions.md)
* [Virtual Destructors](068-VirtualDestructors/VirtualDestructors.md)
* [Casting in C++](069-Casting/Casting.md)

The number on the folders are the index of the videos on the YouTube Playlist, not every folder have a Visual Studio project, most of the time I just take some notes.

0 comments on commit 8422cb5

Please sign in to comment.