【问题标题】:Can I omit non-public inheritance for forward-declaration of classes?我可以为类的前向声明省略非公共继承吗?
【发布时间】:2020-02-28 21:09:26
【问题描述】:

假设我有一段这样的代码:

// Foo.h:
class Incomplete; // the forward-declaration
class Foo {
  void bar(Incomplete&); // doesn't really matter
};
// Foo.cpp:
class Incomplete : private Baz {
};
void Foo::bar(Incomplete&) {
}

Foo.h 这样的前向声明类是否符合标准?如果是,从哪个语言版本开始? protected 继承又如何呢?

【问题讨论】:

    标签: c++ inheritance standards forward-declaration private-inheritance


    【解决方案1】:

    类的前向声明是必需以省略继承。你不能写

    class Incomplete : private Baz;
    

    即使你愿意。

    前向声明的目的是简单地表明特定命名空间中的特定名称引用一个类。指定基类是定义的一部分,因为它提供了有关该类在内存中的布局的信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多