Compare commits

..

No commits in common. "cfd8dd39927a6eaf32b0b1047e1e86dd83a03965" and "8c46e7a977ef1c519595a820e122ae6740ac413e" have entirely different histories.

2 changed files with 16 additions and 136 deletions

View File

@ -44,7 +44,6 @@ catch (int e)
********************************************************************/ ********************************************************************/
#include <iostream> #include <iostream>
#include <typeinfo>
using namespace std; using namespace std;
@ -75,66 +74,52 @@ public:
int main() int main()
{ {
Animal *ap = new Animal(); // 创建一个 Animal 类型的指针 Animal *ap = new Animal(); // 创建一个 Animal 类型的指针
try try
{ {
Cat *cp = dynamic_cast<Cat *>(ap); // 将其转换为 Cat 类型的指针 Cat *cp = dynamic_cast<Cat *>(ap); // 将其转换为 Cat 类型的指针
if (cp)
cp->meow(); cp->meow();
else
throw 1;
} }
catch (int &e) catch (int e)
{ {
cout << "dynamic_cast 将 Animal 转换为 Cat 失败" << endl; cout << "dynamic_cast 将 Animal 转换为 Cat 失败" << endl
<< e << endl;
} }
Animal *ap2 = new Animal(); // 创建一个 Animal 类型的指针 Animal *ap2 = new Animal(); // 创建一个 Animal 类型的指针
try try
{ {
Dog *dp = dynamic_cast<Dog *>(ap2); // 将其转换为 Dog 类型的指针 Dog *dp = dynamic_cast<Dog *>(ap2); // 将其转换为 Dog 类型的指针
if (dp)
dp->bark(); dp->bark();
else
throw 1;
} }
catch (int &e) catch (int e)
{ {
cout << "dynamic_cast 将 Animal 转换为 Dog 失败" << endl; cout << "dynamic_cast 将 Animal 转换为 Dog 失败" << endl
<< e << endl;
} }
Cat *cp2 = new Cat(); // 创建一个 Cat 类型的指针 Cat *cp2 = new Cat(); // 创建一个 Cat 类型的指针
try try
{ {
Animal *ap3 = dynamic_cast<Animal *>(cp2); // 将其转换为 Animal 类型的指针 Animal *ap3 = dynamic_cast<Animal *>(cp2); // 将其转换为 Animal 类型的指针
if (ap3)
cout << "dynamic_cast 将 Cat 转换为 Animal 成功" << endl; cout << "dynamic_cast 将 Cat 转换为 Animal 成功" << endl;
else
throw 1;
} }
catch (int &e) catch (int e)
{ {
cout << "dynamic_cast 将 Cat 转换为 Animal 失败" << endl; cout << "dynamic_cast 将 Cat 转换为 Animal 失败" << endl
<< e << endl;
} }
Dog *dp2 = new Dog(); // 创建一个 Dog 类型的指针 Dog *dp2 = new Dog(); // 创建一个 Dog 类型的指针
try try
{ {
Animal *ap4 = dynamic_cast<Animal *>(dp2); // 将其转换为 Animal 类型的指针 Animal *ap4 = dynamic_cast<Animal *>(dp2); // 将其转换为 Animal 类型的指针
if (ap4)
cout << "dynamic_cast 将 Dog 转换为 Animal 成功" << endl; cout << "dynamic_cast 将 Dog 转换为 Animal 成功" << endl;
else
throw 1;
} }
catch (int &e) catch (int e)
{ {
cout << "dynamic_cast 将 Dog 转换为 Animal 失败" << endl; cout << "dynamic_cast 将 Dog 转换为 Animal 失败" << endl
<< e << endl;
} }
delete ap;
delete ap2;
delete cp2;
delete dp2;
return 0; return 0;
} }

View File

@ -1,105 +0,0 @@
#include <iostream>
#include <typeinfo>
class Animal
{
public:
virtual ~Animal() {}
};
class Cat : public Animal
{
public:
void meow()
{
std::cout << "Meow!" << std::endl;
}
};
class Dog : public Animal
{
public:
void bark()
{
std::cout << "Woof!" << std::endl;
}
};
int main()
{
// 任务1将Animal类型指针转换为Cat类型指针
Animal *animal1 = new Cat();
try
{
Cat *cat = dynamic_cast<Cat *>(animal1);
if (cat)
{
cat->meow();
}
else
{
throw 1;
}
}
catch (int e)
{
std::cout << "转换失败无法将Animal类型转换为Cat类型。" << std::endl;
}
// 任务2将Animal类型指针转换为Dog类型指针
Animal *animal2 = new Dog();
try
{
Dog *dog = dynamic_cast<Dog *>(animal2);
if (dog)
{
dog->bark();
}
else
{
throw 2;
}
}
catch (int e)
{
std::cout << "转换失败无法将Animal类型转换为Dog类型。" << std::endl;
}
// 任务3将Cat类型指针转换为Animal类型指针
Cat *cat2 = new Cat();
try
{
Animal *animal3 = dynamic_cast<Animal *>(cat2);
if (animal3)
{
std::cout << "转换成功Cat类型成功转换为Animal类型。" << std::endl;
}
}
catch (const std::bad_cast &e)
{
std::cout << "转换失败:" << e.what() << std::endl;
}
// 任务4将Dog类型指针转换为Animal类型指针
Dog *dog2 = new Dog();
try
{
Animal *animal4 = dynamic_cast<Animal *>(dog2);
if (animal4)
{
std::cout << "转换成功Dog类型成功转换为Animal类型。" << std::endl;
}
}
catch (const std::bad_cast &e)
{
std::cout << "转换失败:" << e.what() << std::endl;
}
// 释放内存
delete animal1;
delete animal2;
delete cat2;
delete dog2;
return 0;
}