class Animal {
public:
Animal(const string& name)
:name_(name)
{
cout << "Animal(string& name) " << endl;
}
~Animal() {
cout << "~Animal()" << endl;
}
virtual void showInfo() = 0;
string getName() {
return name_;
}
private:
string name_;
};
class Cat : public Animal {
public:
Cat(const string& owner, const string& name)
: owner_(owner)
, Animal(name)
{
cout << "Cat(string& owner, string& name)" << endl;
}
~Cat() {
cout << "~Cat()" << endl;
}
virtual void showInfo() {
cout << "owner : " << owner_ << " Cat name:" << getName() << endl;
}
private:
string owner_;
};
我们用支持RTTI的dynamic_cast对裸指针进行向下转换
int main() {
Animal* animal_p = new Cat("zhang", "bsm");
Cat* cat_p = dynamic_cast<Cat*>(animal_p); // 只有当animal_p指向Cat对象时,才能成功。转换失败时,返回nullptr
delete cat_p;
return 0;
}
同样的,我们也可以用支持RTTI的dynamic_pointer_cast对智能指针进行向下转换
shared_ptr<Animal> pa(new Cat("zhang", "bsm"));
shared_ptr<Cat> pc(dynamic_pointer_cast<Cat>(pa)); // 转换失败时,返回nullptr
在进行向上转换时,dynamic_cast和static_cast一样,dynamic_pointer_cast和static_pointer_cast一样。但进行向下转换时,dynamic_cast和static_cast不一样,dynamic_pointer_cast和static_pointer_cast也不一样
Animal* animal_p = new Cat("zhang", "bsm");
Cat* cat_p = static_cast<Cat*>(animal_p); // 虽然转换成功,但这并不安全
Dog* dog_p = dynamic_cast<Dog*>(animal_p); // 转换失败,只有当animal_p指向Cat对象时,才能成功
shared_ptr<Animal> pa(new Cat("zhang", "bsm"));
shared_ptr<Dog> pc(dynamic_pointer_cast<Dog>(pa)); // 转换失败,RTTI识别不匹配
shared_ptr<Dog> pc(static_pointer_cast<Dog>(pa)); // 虽然转换成功,但这并不安全
静态转换可以在继承结构中任意转换,但这并不安全