什么是C ++中的多态类型? [英] what's polymorphic type in C++?

查看:204
本文介绍了什么是C ++中的多态类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一篇文章中发现static_cast用于非多态类型转换,而dynamic_cast用于多态类型转换。我理解int和double不是多态类型。

I found in one article saying "static_cast is used for non-polymorphic type casting and dynamic_cast is used for polymorphic type casting". I understand that int and double are not polymorphic types.

但是,我还发现static_cast可以在基类和派生类之间使用。这里的多态类型是什么意思?有些人说多态类型意味着有虚函数的基类。是对的吗?这是唯一的情况吗?还有什么?

However, I also found that static_cast can be used between base class and derived class. What does polymorphic type here mean? Some people says polymorphic type means the base class with virtual function. Is that right? Is this the only situation? What else? Can anybody could elaborate this for me more?

推荐答案

首先,文章不完全正确。 dynamic_cast 检查对象的类型,可能会失败,static_cast 不会检查,并且在很大程度上要求程序员知道他们在做什么严重错误),但它们都可用于多态性情境。 ( dynamic_cast 具有至少一个涉及的类型具有虚拟方法的额外要求。)

First of all, the article is not completely correct. dynamic_cast checks the type of an object and may fail, static_cast does not check and largely requires the programmer to know what they're doing (though it will issue compile errors for some egregious mistakes), but they may both be used in polymorphic situations. (dynamic_cast has the additional requirement that at least one of the involved types has a virtual method.)

C ++中的多态性简而言之,是通过单独定义的界面使用对象。该接口是基类,并且它几乎总是只有当它有虚拟方法时才有用。

Polymorphism in C++, in a nutshell, is using objects through a separately-defined interface. That interface is the base class, and it is almost always only useful to do this when it has virtual methods.

但是,它是罕见的,但可能有多态性任何虚拟方法;往往这是一个坏的设计或不得不满足外部要求的标志,因此,没有办法给出一个很好的例子,将适合这里。 (你会知道什么时候使用它,当你看到,不幸的是,这是最好的建议,我可以给你在这里。)

However, it's rare-but-possible to have polymorphism without any virtual methods; often this is a sign of either bad design or having to meet external requirements, and because of that, there's no way to give a good example that will fit here. ("You'll know when to use it when you see it," is, unfortunately, the best advice I can give you here.)

多态性的例子: p>

Polymorphism example:

struct Animal {
  virtual ~Animal() {}
  virtual void speak() = 0;
};

struct Cat : Animal {
  virtual void speak() { std::cout << "meow\n"; }
};

struct Dog : Animal {
  virtual void speak() { std::cout << "wouf\n"; }
};

struct Programmer : Animal {
  virtual void speak() {
    std::clog << "I refuse to participate in this trite example.\n";
  }
};

稍微练习上述类别,也可以看到我的通用工厂示例

Exercising the above classes slightly—also see my generic factory example:

std::auto_ptr<Animal> new_animal(std::string const& name) {
  if (name == "cat") return std::auto_ptr<Animal>(new Cat());
  if (name == "dog") return std::auto_ptr<Animal>(new Dog());
  if (name == "human") return std::auto_ptr<Animal>(new Programmer());
  throw std::logic_error("unknown animal type");
}

int main(int argc, char** argv) try {
  std::auto_ptr<Animal> p = new_animal(argc > 1 ? argv[1] : "human");
  p->speak();
  return 0;
}
catch (std::exception& e) {
  std::clog << "error: " << e.what() << std::endl;
  return 1;
}

也可以使用无继承的多态性,因为它真的是一种设计 / em>。 (我拒绝在此处使用流行语 :P)

It's also possible to use polymorphism without inheritance, as it's really a design technique or style. (I refuse to use the buzzword pattern here... :P)

这篇关于什么是C ++中的多态类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆