cast in cpp code example
Example 1: c++ cast operator
struct X {
operator int() const { return 7; }
explicit operator int*() const { return nullptr; }
using arr_t = int[3];
operator arr_t*() const { return nullptr; }
};
int main()
{
X x;
int n = static_cast<int>(x);
int m = x;
int* p = static_cast<int*>(x);
int (*pa)[3] = x;
}
Example 2: cast c++
#include <iostream>
using namespace std;
int main(){
int x = 4;
int y = 2;
cout<<"La divisione dei valori e': "<<(float)y/x<<endl;
}
Example 3: c++ casting
static_cast:
void* data;
pointer *pData = static_cast<pointer*>(data);
const_cast:
const char* characters;
const_cast<char*>(characters);
reinterpret_cast:
struct S1 { int a; } s1;
int* p1 = reinterpret_cast<int*>(&s1);