(new-type) expression new-type (expression)
char c = 10; // 1 个字节 int *p = (int *)&c; // 4 个字节(32bit platform) *p = 5; // 内存踩脏 int *q = static_cast<int *>(&c); // 使用static_cast可在编译阶段将该错误检查出来。
#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;
}
#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;
}
#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;
}
#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;
}
----------------
/ dynamic_cast \ -->同一继承体系(virtual)的类指针或引用[更安全的downcast]
~~~~~~~~~~~~~~~~~~~~
/ static_cast \ -->基础类型[更安全],同一继承体系的类指针或引用
~~~~~~~~~~~~~~~~~~~~~~~~
/ reinterpret_cast \ -->与C-Like的作用一致,没有任何静态或者动态的checking机制
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/ C-Like \ -->基础类型,同一继承体系的类指针或引用,不同继承体系类的指针或引用
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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 int myConst = 15; int *nonConst = const_cast<int *>(&myConst); *nonConst = 10; // 如果该变量存放在read-only内存区当中,在运行时可能会出现错误。
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有