C++?????е????????????
???????????? ???????[ 2015/8/5 11:01:44 ] ????????.NET ???????
????1??????
???????????????StackOverflow??????????????????????????????C/C++??????????????????????о?????????????????????????????????????????????????????????Щ???????????????????????????????????????Щ??
???????????????C++ ???????????????μ????????????????static_cast?? dynamic_cast?? reinterpret_cast??????const_cast????????????ЩC++?????У??????????????????顣???????????????????C-like??????????????????????д????????????C-Like?????????????????????????????????static_cast?? const_cast??reinterpret_cast?????????????????????????????
???????????????C-Like????????????????μ???????????????????????????????
????(new-type) expression
????new-type (expression)
????2??static_cast vs dynamic_cast
??????????static_cast??dynamic_cast???????????????????????????????????????????????????????????????????????????“??”?“??”??????????????????????????????????????????????dynamic_cast??Run-time Checkingt??static_cast????????????????????????
????static_cast?? 1?????????????????2??????????????????????
????dynamic_cast?????????????????????????????????????????
????2.1 ??C-Like??static_cast
????static_cast?????????????int?? float?? char???????????????????????????????C-Like??????????????static_cast?????????????
????char c = 10; // 1 ?????
????int *p = (int *)&c; // 4 ??????32bit platform??
????*p = 5; // ??????
????int *q = static_cast<int *>(&c); // ???static_cast????????ν?????????????
?????????????????????????C-Like?????????????????????????????????????????????????????????????????C-Like?????????????????????
#include <iostream>
class A
{
public:
A(){}
~A(){}
private:
int i?? j;
};
class C
{
public:
C(){}
~C(){}
void printC()
{
std::cout <<"call printC() in class C" <<std::endl;
}
private:
char c1?? c2;
};
int main()
{
A *ptrA = new A;
//C *ptrC = static_cast<C *>(ptrA);
// ?????????????????
// In function ‘int main()’:
// error: invalid static_cast from type ‘A*’ to type ‘C*’
C *ptrC = (C *)(ptrA);
ptrC->printC();
// ?????????????
// ?????????????????????printC???????????????????????“undefined”
// ??????????????Щ???????????????????????????????????
// ???????????????????????????????????
return 0;
????}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11