/*...................Ex.2 static_cast ........................Referance code from website : https://www.geeksforgeeks.org/static_cast-in-c-type-casting-operators/*/#include<iostream>usingnamespace std;intmain() {int a =10; char c ='a'; // //address &c convert an int pointer *q with c-type cast. it is pass at compile time, pass at run time// int *q = (int *)&c; // cout<<" address &c convert an int pointer *q with c-type cast = "<<*q <<endl ; //address &c convert an int pointer *q with static_cast pass at compile time, may fail at run time int*q =static_cast<int*>(&c); cout<<" address &c convert an int pointer *q with static_cast = "<<*q <<endl ; /* run coed ./Ex2_static_cast output error complie time error: invalid static_cast from type ‘char*’ to type ‘int*’ int* p = static_cast<int*>(&c); */}
/* ex3*/// https://qastack.in.th/programming/2253168/dynamic-cast-and-static-cast-in-c#include<iostream>#include<string>usingnamespace std; classB {};classD:publicB {};classX {};intmain(){ D* d =new D; B* b =static_cast<B*>(d); // this works cout<< b<<endl; // X* x = static_cast<X*>(d); // ERROR - Won't compilereturn0;}
Assoc. Prof. Wiroon Sriborrirux, Founder of Advance Innovation Center (AIC) and Bangsaen Design House (BDH), Electrical Engineering Department, Faculty of Engineering, Burapha University