简体   繁体   English

c ++我应该使用前向声明吗?

[英]c++ Should I use forward declaration?

I found this question When to use forward declaration? 我发现这个问题什么时候使用前向声明? which is useful, but it is descriptive not prescriptive. 这是有用的,但它是描述性的而不是规定性的。

My scenario is typically I'm using a pointer to another class either as a class member or function argument so all I need in the header is a forward declaration (also as an aside, if I were to switch to using boost shared_ptr's would they be compatible with using forward declarations?). 我的场景通常是我使用指向另一个类的指针作为类成员或函数参数,所以我在标题中需要的只是一个前向声明(另外,如果我切换到使用boost shared_ptr,它们将是与使用前向声明兼容?)。 Currently I'm just including the headers but now I'm wondering if I should use forward declarations. 目前我只是包括标题,但现在我想知道我是否应该使用前向声明。

So my question is, if I can use a forward declaration for a class, should I? 所以我的问题是,如果我可以使用前瞻性声明,我应该吗? I'm hoping this question is not subjective, but if there is not a best practice answer then what are the pros/cons of using forward declarations? 我希望这个问题不是主观的,但如果没有最佳实践答案,那么使用前瞻性声明的利弊是什么?

UPDATE UPDATE

Just to expand on the shared_ptr issue (I'm not using them now but considering the switch). 只是为了扩展shared_ptr问题(我现在没有使用它们,而是考虑切换)。 If I were to use them I think I'd use the practice of typedef'ing the shared_ptr type inside the class. 如果我要使用它们,我想我会在类中使用typedef的shared_ptr类型。 Eg: 例如:

class Customer
{
public:
    typedef std::tr1::shared_ptr<Customer> SharedPointer;

    Customer() {}   
    virtual ~Customer() {}

    virtual std::string GetName() const;
};

Seems like that might make things messier. 似乎这可能会使事情变得更加混乱。 Would that be problematic with forward declaration and if so is there a simple workaround? 这会对前向声明产生问题,如果是这样,有一个简单的解决方法吗?

You might want to do that because including the files makes the compilation longer, but depending on how many cases you have and how big your code base is, it most likely won't be an issue. 您可能希望这样做,因为包含文件会使编译时间更长,但根据您拥有的案例数量和代码库的大小,它很可能不会成为问题。

Another thing to consider is the dependencies. 另一件需要考虑的事情是依赖关系。 You don't want to recompile all your code because of an include file that you changed, when all you need is just the pointer definition. 您不希望重新编译所有代码,因为您更改了包含文件,而您只需要指针定义。

So my (subjective) answer would be Yes, you should . 所以我的(主观)答案是肯定的,你应该

Yes, you should. 是的你应该。 Remember that anything you include in a header is also included when clients of your code use it. 请记住,当您的代码的客户端使用它时,您还包括在标题中包含的任何内容。 It is best to include only the minimum number of files needed in a header. 最好只包含标头中所需的最小文件数。 Aside from that, if you don't need to include the entire file and a forward declaration is adequate it seems like a simple choice. 除此之外,如果您不需要包含整个文件并且前向声明足够,那么它似乎是一个简单的选择。

我认为最好使用前向声明来提高编译速度(不包括大量文件)使用共享指针的问题它不会改变任何东西,因为共享指针只是一个包装器,负责清理堆内存

They usually say that you should use forward declarations wherever possible. 他们通常会说你应该尽可能使用前瞻声明。 This rule, like all rules, has exceptions. 与所有规则一样,此规则也有例外。 For me, the exceptional cases are often when the type name is overly complicated (ie template ), or when there are too many names. 对我来说,例外情况通常是类型名称过于复杂(即模板 ),或者名称太多时。 For example, the following is a forward declaration: 例如,以下是前向声明:

namespace foo
{
    namespace bar
    {
        template <typename T1, typename T2, int X>
        class MyNiftyType;
        // Hmm, maybe declare more types here?
    }
    // Hmm, maybe declare even more types here?
}

If that stuff could be avoided by doing just #include "MyNiftyStuff.h" , i'd better #include ! 如果通过#include "MyNiftyStuff.h"可以避免这些东西,我会更好#include

BTW there is a standard header file <iosfwd> that contains forward declarations for some stream types. BTW有一个标准头文件<iosfwd> ,它包含某些流类型的前向声明。 It seems to be invented specifically so you are able to declare operator<<(std::ostream&, ...) (it's my personal opinion, sorry if it's wrong after all). 它似乎是专门发明的,所以你能够声明operator<<(std::ostream&, ...) (这是我的个人意见,对不起,如果它毕竟是错的)。


Edit: regarding shared_ptr<type> . 编辑:关于shared_ptr<type>

Roughly speaking, the only thing you can do with shared pointers (to forward-declared types) is declaring functions. 粗略地说,你可以用共享指针(向前声明的类型)做的唯一事情是声明函数。 If you want to define a function that does something useful with shared_ptr<type> , you cannot just forward-declare type . 如果要定义一个对shared_ptr<type>执行某些有用操作的函数,则不能只是转发声明type For example: 例如:

MyCode.h MyCode.h

class MyClass;
void DoMuchStuff(shared_ptr<MyClass> ptr); // declaration - OK
inline void DoDoubleStuff(shared_ptr<MyClass> ptr) // definition - not OK!
{
    void DoMuchStuff(ptr);
    void DoMuchStuff(ptr);
}

If i used a normal pointer instead of shared_ptr , this would work with forward declarations. 如果我使用普通指针而不是shared_ptr ,这将适用于前向声明。 But this inconvenience rarely hits you - either you have just declarations in your .h files, or your inline functions are sufficiently complex that you have to #include the full declaration for your class anyway. 但是这种不便很少打击你 - 要么你的.h文件中只有声明,要么你的内联函数足够复杂,你必须#include你的课程的完整声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM