????2.2 static_cast?????????????????
???????????С??????????static_cast??C-Like??????????????????????????????????????Χ??С?????????????е?????????????????????????????????????????????????????
????static_cast??????????????????????????????????upcast????????downcast???????????????upcast?????????????????????????????и??????????????????????????????????????????????????????????????????????????????static_cast?????C-Like?????????????Ч???????????????????????????????????????????й??????
?????????????????static_cast????downcast???????????????????????????????????????????????δ?????????
#include <iostream>
class A
{
public:
A():i(1)?? j(1){}
~A(){}
void printA()
{
std::cout <<"call printA() in class A" <<std::endl;
}
void printSum()
{
std::cout <<"sum = " <<i+j <<std::endl;
}
private:
int i?? j;
};
class B : public A
{
public:
B():a(2)?? b(2) {}
~B(){}
void printB()
{
std::cout <<"call printB() in class B" <<std::endl;
}
void printSum()
{
std::cout <<"sum = " <<a+b <<std::endl;
}
void Add()
{
a++;
b++;
}
private:
double a?? b;
};
int main()
{
B *ptrB = new B;
ptrB->printSum();
//????????sum = 4
A *ptrA = static_cast<B *>(ptrB);
ptrA->printA();
ptrA->printSum();
//????????sum = 2
//?????upcast?????????????????????????????????
ptrA = new A;
ptrA->printSum();
//????????sum = 2
ptrB = static_cast<B *>(ptrA);
ptrB->printB();
ptrB->printSum();
//????????sum = 0
//?????downcast????????????“undefined”??
//B b;
//B &rB = b;
//rB.printSum();
//????????sum = 4
//A &rA = static_cast<A &>(rB);
//rA.printA();
//rA.printSum();
//????????sum = 2
//?????upcast???????????????????????????????????
//A a;
//A &rA = a;
//rA.printSum();
//????????sum = 4
//B &rB = static_cast<B &>(rA);
//rB.printB();
//rB.printSum();
//????????sum = 5.18629e-317
//?????downcast????????????“undefined”??
return 0;
}
?????????static_cast?????????????????????downcast?????????C-Like??????????????????????????????????????????δ?????????????t????????static_cast???downcast????????????????t????????????????????????????????y???downcast?????
???????????????????????????????????в??????downcast????????????????????????????????????downcast???????????????е???????У???????dynamic_cast??????
????2.3 dynamic_cast
????dynamic_cast??????????downcast???????????????????????????????
????downcast????????????????“????”????
????????????????????????????????????
????dynamic_cast????upcast??static_cast??Ч????????????????dynamic_cast??????RTTI???????????????????static_cast????
#include <iostream>
#include <exception>
class A
{
public:
virtual void print()
{
std::cout <<"Welcome to WorldA!" <<std::endl;
}
};
class B : public A
{
public:
B():a(0)?? b(0) {}
~B(){}
virtual void print()
{
std::cout <<"Welcome to WorldB!" <<std::endl;
}
private:
double a?? b;
};
int main()
{
B *ptrB = new B;
A *ptrA = dynamic_cast<A *>(ptrB);
ptrA->print();
//?????е??У??????????upcast?dynamic_cast?????Ч????static_cast???
//????????virtual???????????????????????????
//A *ptrA = new A;
//B *ptrB = dynamic_cast<B *>(ptrA);
//ptrB->print();
//Segmentation fault???????????downcast???????????????NULL??
//A a;
//A &ra = a;
//B &b = dynamic_cast<B &>(ra);
//b.print();
//???St8bad_cast??????????????downcast??????????????????
//ptrA = new A;
//ptrB = static_cast<B *>(ptrA);
//ptrB->print();
//???static_cast????downcast???????dynamic_cast????NULL?????
//????????ptrB????????????麯????
//ptrA = new A;
//ptrB = dynamic_cast<B *>(ptrA);
//ptrB->print();
//?????downcast?????????virtual?????????????????????
// In function ‘int main()’:
// cannot dynamic_cast ‘ptrA’ (of type ‘class A*’) to type ‘class B*’ (source type is not polymorphic)
return 0;
}
??????????????????????????г????£???????dynamic_cast??????????????static_cast?????dynamic_cast??????????????????????????д??????????????????????????????????????????????
????3??reinterpret_cast
????reinterpret_cast??Σ??????cast??????????Σ????????????????C-Like?????????????????????????????Щlow-level?????????λ???????????á?
#include <iostream>
class A
{
public:
A(){}
~A(){}
void print()
{
std::cout <<"Hello World!" <<std::endl;
}
};
class B
{
public:
B():a(0)?? b(0) {}
~B(){}
void call()
{
std::cout <<"Happy for your call!" <<std::endl;
}
private:
double a?? b;
};
int main()
{
//A *ptrA = new A;
//B *ptrB = reinterpret_cast<B *>(ptrA);
//ptrB->call();
//????????
//A *ptrA = new A;
//B *ptrB = (B *)(ptrA);
//ptrB->call();
//????????
//A *ptrA = new A;
//B *ptrB = static_cast<B *>(ptrA);
//ptrB->call();
//??????????????
//In function ‘int main()’:
//error: invalid static_cast from type ‘A*’ to type ‘B*’
//char c;
//char *pC = &c;
//int *pInt = static_cast<int *>(pC);
//???????????error: invalid static_cast from type ‘char*’ to type ‘int*’
//int *pInt = reinterpret_cast<int *>(pC);
//????????
//int *pInt = (int *)(pC);
//????????
return 0;
}
??????????static_cast??dynamic_cast??reinterpret_cast????????????μ???????????????????м?????????????н?const_cast?????????????????????????????????н????
----------------
/   dynamic_cast -->??????????virtual??????????????[???????downcast]
~~~~~~~~~~~~~~~~~~~~
/     static_cast    -->????????[?????]??????????????????????
~~~~~~~~~~~~~~~~~~~~~~~~
/    reinterpret_cast    -->??C-Like????????£?????κξ??????????checking????
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/          C-Like            -->??????????????????????????????????????????????????
????~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
????4??const_cast
????const_cast????????????????????????????const????????????????????const_cast??????C?????????????????????????const?????????????????????????????????????????????????????C++???????????????????const??????????????????????????????????????????????????????ν?????????????????cast const????????????磺
????const int myConst = 15;
????int *nonConst = const_cast<int *>(&myConst);
????void print(int *p)
????{
????std::cout << *p;
????}
????print(&myConst); // ???????error: invalid conversion from ‘const int*’ to ‘int*’
????print(nonConst); // ????
???????????????const_cast?????????????????б????????????????????
????const int myConst = 15;
????int *nonConst = const_cast<int *>(&myConst);
????*nonConst = 10;
????// ?????????????read-only????????У???????????????????
????5??С??
??????C++???ж?????????????????????C-Like???????????????????????????????????????????????????????????????????????C++?漲???????????????????????????????????????
??????????C++?????“??”???????????????????????????????????????????
??????????????C-Like?????????????????????????????????????????????????????????????????????????Щ?????????????????????????
????????C++??????????????????????????????????????????????Щ?????????????????????????reinterpret_cast???????????????????????C-Like????????????????static_cast????????????????????????????????????char *????int *??????????????????????????????????????????dynamic_cast???????????????μ??????downcast?????????????????????????????????????????......
???????????????????????????????????const_cast?? static_cast?? reinterpret_cast??dynamic_cast??????????????????C-Like???????ɡ??????C-Like???????static_cast?? dynamic_cast?????????????????????????????
????C++???Bjarne Stroustrup???????????????????????????????????C-Like??cast???????????????????????????????????????????????????????cast??ó?????????????????????????????????????????????????μ?cast????????????淶???????ó??????д?????????cast??