0

可能重复:
在 C++ 中使用 Static_cast 向下转换

我很困惑我们什么时候需要将指向基对象的指针转换为派生类的指针?谁能帮我举个例子?

4

4 回答 4

1

你在说什么样的转换

如果它是关于向下转换(或动态转换,它是同一类型但在运行时验证),那么您可以通过强制执行您指定的类型来强制类型检查器不检查该特定类型。

这意味着它是代码内部的潜在中断,该特定指令中缺少保证的类型安全性,但有时需要它,即使一个好的设计不应该先验地需要它。

必要性是因为如果没有强制转换,即使您确定指向基类的指针包含派生类,也不允许调用派生类的任何方法(但编译器无法验证它) )。

于 2012-11-21T03:45:56.930 回答
1

WHEN

so heres an example:

say you have a factory (an animal factory) that you can send it a string or an enum of what type of object you want...

"Hey Animal Factory, give me a 'Dog'"

there are two ways to write your factory...

You could write a function for every animal like so (good programmers dont do this):

  • Dog* GetDog();
  • Cat* GetCat();
  • Etc

or you could write a factory with one function like so (dont think about generics for now):

  • Animal* GetAnimal(string animalType);

You know that Animal is actually a dog or cat so you can typecast it to what you need it to be WHEN you need to access the members of Dog or Cat...

My Favorite thing to do in C++ is the temporary cast to get into something (excuse my C++ its rusty)

Animal* myAnimal = Factory.GetAnimal("Dog");
int barkVolume = ((Dog*)myAnimal).BarkVolume;
于 2012-11-21T04:09:04.430 回答
1

Imagine you have a base class Animal and two derived classes, Cat and Dog.

// Assume methods are public.
class Animal { virtual void Attack(); }
class Dog : public Animal { void Meow(); }
class Cat : public Animal { void Bark(); }

We can use base class pointers to reference our derived objects. This helps as we can now contain them as the same type.

Animal* myCat = new Cat;
Animal* myDog = newDog;
std::vector<Animal*> myAnimals;
myAnimals.push_back(myCat);
myAnimals.push_back(myDog);

This is useful, as we can call base member functions all all kinds of animals, regardless of their derived class types.

// Call Attack() on every cat and dog.
for_each(myAnimals.begin(), myAnimals.end(), [](auto& e) { e->Attack(); });

You can use dynamic casting to test if one of the base pointers can be converted into a derived class pointer.

Cat* realCat = dynamic_cast<Cat*> myAnimals[0]; // Success. Pointer is valid.
Cat* fakeCat = dynamic_cast<Cat*> myAnimals[1]; // Failure, it's not a cat. NULL.

You can now call your member methods, such as Meow() from the derived class pointers. This was not possible before, as Animal does not have these methods.

realCat->Meow(); // Valid.
myCat->Meow(); // Animal*, there is not Meow() method.
于 2012-11-21T04:10:10.373 回答
0

您需要这样做才能访问基类中不存在的派生类成员。不过,有了良好的编程实践和泛型,您可能不应该将指针类型化为基类。

于 2012-11-21T03:46:04.943 回答