基类列表是指示虚拟继承的正确位置吗?

发布于 2024-09-13 21:10:34 字数 938 浏览 5 评论 0原文

我从未见过用作虚拟和非虚拟基类的类(即,如果某个类旨在成为祖先,那么我们通常会提前知道继承类型 - 虚拟或非虚拟)。

所以我认为 c++ 中有一个容易出错的自由来专门化基类列表中的“虚拟”继承。最好将基类本身指定为“虚拟”

或者我错了?

如果没有,任何人都可以描述一些技术来防止这种“虚拟”类的意外非虚拟继承吗?

或者对即将到来的 C++ 标准有一些看法?

(抱歉,如果重复)


一些示例

1)ReferenceCounted 类作为某些基于引用计数的智能指针可以指向的所有类的基类。我们需要防止此基本实例(和引用计数器)的重复。除了优化之外,没有任何理由使用此类作为非虚拟基。

2)接口层次结构和相应的实现层次结构 (在这种情况下,接口层次结构必须是“虚拟的”)

// interfaces:

struct BaseIface{
  void virtual func()=0;
};

struct DerivedIface: public virtual BaseIface{
  void some_another_func()=0;
}


// implementations

class BaseImpl: public virtual BaseIface{
  void virtual func(){....};
}

class DerivedImpl: public BaseImpl, public virtual DerivedIface{
  void some_another_func(){...};
}

我怀疑在许多情况下非虚拟继承不是概念上的需要,它仅用于减少虚拟继承开销(有时用于 static_cast<> 驱动的能力:)

注意,Java 仅使用虚拟(就 C++ 而言)接口继承,而且我不知道有任何人抱怨这种语言缺乏“非虚拟”(它本质上是比 C++ 表达能力差的语言,但这个“功能”并不是它的主要错误:)。

I have never seen a class used as virtual and nonvirtual base (i.e. if some class is intended to be an ancestor then we usually know in advance about type of inheritance - virtual or nonvirtual).

So I suppose that there is an error-prone freedom in c++ to specialize "virtual" inheritance in base class list. It should be better to specify as "virtual" the base class itself

Or maybe I'm wrong?

If no, can anybody describe some technics to prevent accidental nonvirtual inheritance for such a "virtual" class?

Or there are some perspectives in upcoming c++ standards?

(Sorry if duplicate)


Some examples

1) ReferenceCounted class as base for all classes that some reference-count-based smartpointer can point to. We need to prevent duplicates of this base instances (and reference counters). There are no reasons to use this class as nonvirtual base, except of optimization.

2) A hierarchy of interfaces and corresponding hierarchy of implementations
(interfaces hierarchy must be "virtual" in this case)

// interfaces:

struct BaseIface{
  void virtual func()=0;
};

struct DerivedIface: public virtual BaseIface{
  void some_another_func()=0;
}


// implementations

class BaseImpl: public virtual BaseIface{
  void virtual func(){....};
}

class DerivedImpl: public BaseImpl, public virtual DerivedIface{
  void some_another_func(){...};
}

I suspect that in many cases nonvirtual inheritance is not a conceptual need, it used only to reduce virtual inheritance overhead (and sometimes for an ability to static_cast<> to drived :)

Note, that Java used ONLY virtual (in terms of c++) inheritance for interfaces, and I don't know any complains that this language lacks "nonvirtual" (it is esentially less expressive language than c++ but this "feature" is not it's main fault :).

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

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

发布评论

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

评论(1

憧憬巴黎街头的黎明 2024-09-20 21:10:34

在基类中没有太多方法可以做到这一点(你也不想这样做)。对于虚拟继承和非虚拟继承使用基类是完全合理的。

您真正喜欢是在最派生的类中指定虚拟继承,而当前必须在中间类中指定虚拟继承。不幸的是,我没有看到太多解决方法——尽管当一个类派生自两个(或多个)其他类且每个类都有一个共同基础时,虚拟继承变得必要(主要),但虚拟继承实际上控制着这两个其他类的方式类是编译的,因此如果您(仅)在最派生的类中指定它,您最终会得到类似 export 的结果,您可能需要返回并重新编译这些中间类基于指定虚拟继承的最派生类(并且有某种方法来存储以两种方式编译的中间类,因为它可能以其中一种或两种方式使用)。

There's not really much way you could do this in the base class (nor would you really want to). It's perfectly reasonable to use a base class for both virtual and non-virtual inheritance.

What you'd really like would be to specify the virtual inheritance in the most derived class, where currently has to be specified in the intermediate classes. Unfortunately, I don't see much way around that -- even though virtual inheritance becomes necessary (primarily) when a class derives from two (or more) other classes that each have a common base, the virtual inheritance really governs how those two other classes are compiled, so if you (only) specified it in the most derived class, you'd end up with something almost like export, where you might need to go back and re-compile those intermediate classes based on the most derived class specifying virtual inheritance (and have some way to store the intermediate classes compiled both ways since it might be used either or both ways).

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