Forward在c ++中声明了一个类的public typedef

时间:2021-02-26 20:56:57

I'm trying to simplify a bunch of header file "include spaghetti" by using forward declarations and moving #includes into the implementation file. However, I keep coming upon the following scenario:

我正在尝试通过使用前向声明并将#includes移动到实现文件中来简化一堆头文件“include spaghetti”。但是,我继续发现以下情况:

//Foo.h
#include "Bar.h"

class Foo
{
public:
  void someMethod(Bar::someType_t &val);
};

//Bar.h
.
.
.
class Bar
{
public:
  typedef std::vector<SomeClass> someType_t;
};

I want to remove #include "Bar.h" in as many cases as possible. I also see the situation where the typedef in Bar.h is listed outside of the Bar class. I'm assuming both situations can be addressed in the same manner.

我想在尽可能多的情况下删除#include“Bar.h”。我还看到Bar.h中的typedef列在Bar类之外的情况。我假设两种情况都可以用同样的方式解决。

Any ideas?

2 个解决方案

#1


Unfortunately you don't have many choices and none is perfect.

不幸的是,你没有很多选择,没有一个是完美的。

First, the two obvious and unacceptable solutions:

首先,两个明显且不可接受的解决方案:

  • You can forward declare the typedef which totally defeats the purpose of using a typedef.
  • 你可以转发声明typedef,这完全违背了使用typedef的目的。

  • You include the file which contains the typedef, which you want to avoid.
  • 您包含要避免的包含typedef的文件。

The more interesting solutions:

更有趣的解决方案:

  • Have all related typedefs in the same include and include that file. This creates code coupling between the classes though. You ought to do that only with related classes, else you are going to end up with a god include file and this could lead to a lot of recompiling when you add a typedef to that file.
  • 将所有相关的typedef包含在内并包含该文件。这会创建类之间的代码耦合。你应该只使用相关的类来做到这一点,否则你将最终得到一个神包含文件,当你向该文件添加一个typedef时,这可能会导致大量的重新编译。

  • For each class, have a separate include with the typedefs in it. Kind of annoying, but it works.
  • 对于每个类,都有一个单独的include,其中包含typedef。有点讨厌,但它的确有效。

Those last two are like doing forward declarations but with added typedefs. They reduce file interdependencies since you are rarely modifying the typedef file.

最后两个就像做前向声明但添加了typedef。它们减少了文件的相互依赖性,因为您很少修改typedef文件。

I'd say for most situations, the central include has the most benefit for hassle. Just be careful.

我会说,在大多数情况下,*包含对麻烦最有利。小心点

#2


Just use class Bar;. That tells C++ you're declaring your intention to define Bar.

只需使用课程栏;这告诉C ++你宣布你有意定义Bar。

#1


Unfortunately you don't have many choices and none is perfect.

不幸的是,你没有很多选择,没有一个是完美的。

First, the two obvious and unacceptable solutions:

首先,两个明显且不可接受的解决方案:

  • You can forward declare the typedef which totally defeats the purpose of using a typedef.
  • 你可以转发声明typedef,这完全违背了使用typedef的目的。

  • You include the file which contains the typedef, which you want to avoid.
  • 您包含要避免的包含typedef的文件。

The more interesting solutions:

更有趣的解决方案:

  • Have all related typedefs in the same include and include that file. This creates code coupling between the classes though. You ought to do that only with related classes, else you are going to end up with a god include file and this could lead to a lot of recompiling when you add a typedef to that file.
  • 将所有相关的typedef包含在内并包含该文件。这会创建类之间的代码耦合。你应该只使用相关的类来做到这一点,否则你将最终得到一个神包含文件,当你向该文件添加一个typedef时,这可能会导致大量的重新编译。

  • For each class, have a separate include with the typedefs in it. Kind of annoying, but it works.
  • 对于每个类,都有一个单独的include,其中包含typedef。有点讨厌,但它的确有效。

Those last two are like doing forward declarations but with added typedefs. They reduce file interdependencies since you are rarely modifying the typedef file.

最后两个就像做前向声明但添加了typedef。它们减少了文件的相互依赖性,因为您很少修改typedef文件。

I'd say for most situations, the central include has the most benefit for hassle. Just be careful.

我会说,在大多数情况下,*包含对麻烦最有利。小心点

#2


Just use class Bar;. That tells C++ you're declaring your intention to define Bar.

只需使用课程栏;这告诉C ++你宣布你有意定义Bar。