声明但不定义内部结构/类 - 合法的 C++或不?

发布于 2024-08-07 15:15:25 字数 661 浏览 8 评论 0原文

以下代码是否合法 C++?

class Foo
{
  class Bar;

  void HaveADrink(Bar &bar);
  void PayForDrinks(Bar &bar);

  public:
  void VisitABar(int drinks);
};

class Foo::Bar
{
  public:
  int countDrinks;
};

void Foo::HaveADrink(Bar &bar)
{
  bar.countDrinks++;
}
void Foo::PayForDrinks(Bar &bar)
{
  bar.countDrinks = 0;
}
void Foo::VisitABar(int drinks)
{
  Bar bar;
  for (int i=0; i<drinks; i++) HaveADrink(bar);
  PayForDrinks(bar);
}

Visual C++ 和 GCC 都接受它,但是代码对我来说似乎有点奇怪,我不想让它被未来的编译器拒绝。

尽管如此,该模式对我来说似乎对减少编译时间依赖性很有用 - 我经常使用它来声明用于传递一些“上下文”(一堆变量)的结构,这些结构在几个都驻留在同一个 cpp 中的函数之间共享文件,这样我就不必将“上下文”定义引入公共接口中。

Is following code legal C++ or not?

class Foo
{
  class Bar;

  void HaveADrink(Bar &bar);
  void PayForDrinks(Bar &bar);

  public:
  void VisitABar(int drinks);
};

class Foo::Bar
{
  public:
  int countDrinks;
};

void Foo::HaveADrink(Bar &bar)
{
  bar.countDrinks++;
}
void Foo::PayForDrinks(Bar &bar)
{
  bar.countDrinks = 0;
}
void Foo::VisitABar(int drinks)
{
  Bar bar;
  for (int i=0; i<drinks; i++) HaveADrink(bar);
  PayForDrinks(bar);
}

Both Visual C++ and GCC accepts it, however the code seems somewhat strange to me and I would hate to have it refused by some future compiler.

Still, the pattern seems useful to me to reduce compile time dependencies - I often use it to declare structs which are used to pass some "context" (a bunch of variables) which are shared between a few functions which all reside in the same cpp file, and this way I do not have to introduce the "context" definition into the public interface.

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

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

发布评论

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

评论(2

白昼 2024-08-14 15:15:25

合法,并且确实有助于向外界隐藏实施细节。

legal, and indeed usefull to hide implementation details to the outside world.

一袭白衣梦中忆 2024-08-14 15:15:25

[编辑]
我最初说这是“pimpl idiom”: http://c2.com/cgi/wiki?PimplIdiom
但我同意这只是 pimpl 的一部分。 pimpl 使用了这种技术。

您正在将 Bar 类“转发”到 Foo 类中。只要您不在 Foo 的定义内执行任何需要 Bar 大小的操作,就完全合法。您可以使用指针或引用(Bar* 或 Bar&)来引用 Bar,但如果您在 Foo 中声明一个数据成员,如下所示:

private:
酒吧_酒吧;

这是行不通的。原因是因为 Foo 的定义必须足以确定 Foo 的大小。由于 Bar 的大小在 Foo 的定义中是未知的,因此这会使 Foo 的大小不确定。但使用指针是可行的:

私有:
酒吧*_酒吧;

因为指针的sizeof是相同的,因此无论Bar以后如何定义,都是已知的。

[edit]
I originally said this was the "pimpl idiom" : http://c2.com/cgi/wiki?PimplIdiom
but I agree that this is just part of pimpl is about. This technique is used by pimpl.

You're "forwarding" class Bar inside of class Foo. Perfectly legal as long as you don't do anything inside the definigino of Foo that would require the sizeof Bar. You can reference Bar using pointer or reference (Bar* or Bar&), but if you declare a data member in Foo such as this:

private:
Bar _bar;

It wouldn't work. The reason is because the definition of Foo must be enough to determine the sizeof Foo. Since the size of Bar is unknown inside the definition of Foo, it would make the size of Foo indeterminate. But using a pointer would work:

private:
Bar* _bar;

Because the sizeof of pointer is the same, and thus known, regardless of how Bar will be later defined.

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