虚拟函数的行为类似于动态转换

发布于 2025-01-15 22:09:14 字数 353 浏览 4 评论 0原文

Bjarne 在 联合攻击机 C++ 编码标准 中指出:

向下转换(从基类到派生类的转换)只能是 通过以下机制之一允许:

  1. 虚拟函数的行为类似于动态转换(最有可能在 相对简单的情况)
  2. 使用访问者(或类似)模式(最有可能在 复杂的情况)

我无法理解第一个命题。谷歌搜索没有给出任何例子,也没有解释。

虚函数如何像动态强制转换一样起作用?

In JOINT STRIKE FIGHTER AIR VEHICLE C++ CODING STANDARDS Bjarne states, that:

Down casting (casting from base to derived class) shall only be
allowed through one of the following mechanism:

  1. Virtual functions that act like dynamic casts (most likely useful in
    relatively simple cases)
  2. Use of the visitor (or similar) pattern (most likely useful in
    complicated cases)

I can't wrap my head around first proposition. Googling it presented no examples, nor explanation.

How can virtual function act like a dynamic cast?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

留蓝 2025-01-22 22:09:14

它指的是一种在 C++ 早期很常见的技术,当时 dynamic_castRTTI 等被添加到该语言中。

基本思想如下所示:

class Derived1;
class Derived2;

class Base {
public:
    virtual Derived1 *toDerived1() const { return nullptr; }
    virtual Derived2 *toDerivde2() const { return nullptr; }
};

class Derived1 : public Base { 
public:
    Derived1 *toDerived1() const override { return this; }
    Derived2 *toDerived2() const override { return nullptr; }
};

class Derived2 : public Base { 
public:
    Derived1 *toDerived1() const override { return nullptr; }
    Derived2 *toDerived2() const overrode { return this; }
};

这使您可以对指针执行类似 dynamic_cast 的操作,如果被指针对象实际上属于该类型,则生成指向预期类型的​​指针,否则生成 <代码> nullptr 。

但这有明显的缺点。所涉及的每个类都必须了解其他所有类。添加新类意味着向每个类添加新功能。除非整个类结构是完全静态的(并且只有少量类),否则这很快就会变成维护噩梦。

参考

此技术在《Effective C++》第 1 版第 39 条中有所介绍。我还没有进行确认,但它可能已从较新的版本中删除,因为(至少在大多数人看来)程序员)添加 dynamic_cast 使其过时(充其量)。

It's referring to a technique that was kind of common in the early days of C++, before dynamic_cast, RTTI, etc., were added to the language.

The basic idea looks something like this:

class Derived1;
class Derived2;

class Base {
public:
    virtual Derived1 *toDerived1() const { return nullptr; }
    virtual Derived2 *toDerivde2() const { return nullptr; }
};

class Derived1 : public Base { 
public:
    Derived1 *toDerived1() const override { return this; }
    Derived2 *toDerived2() const override { return nullptr; }
};

class Derived2 : public Base { 
public:
    Derived1 *toDerived1() const override { return nullptr; }
    Derived2 *toDerived2() const overrode { return this; }
};

This lets you do something just about like a dynamic_cast on a pointer, yielding a pointer to the expected type if the pointee object is actually of that type, and otherwise a nullptr.

This has obvious disadvantages though. Every class involved has to be aware of every other class. Adding a new class means adding a new function to every class. Unless the entire class structure is quite static (and a small number of classes altogether), this can quickly turn into a maintenance nightmare.

Reference

This technique was covered in Effective C++, 1st Edition, Item 39. I haven't checked to be sure, but it was probably dropped from the newer editions, as (at least in the minds of most programmers) the addition of dynamic_cast rendered it obsolescent (at best).

断念 2025-01-22 22:09:14

您可以使用虚函数进行向下转换,如下所示:

struct Bar;

struct Foo {
  virtual Bar* as_bar() { return nullptr; }
};

struct Bar : Foo {
  Bar* as_bar() override { return this; }
};

在实践中不经常使用。

You can use a virtual function to downcast like this:

struct Bar;

struct Foo {
  virtual Bar* as_bar() { return nullptr; }
};

struct Bar : Foo {
  Bar* as_bar() override { return this; }
};

Not often used, in practice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文