简体   繁体   English

前瞻性声明的目的是什么?

[英]What is the purpose of forward declaration?

what is the description or meaning of this: for example: 这是什么描述或含义:例如:

class test;

class test
{
  .....
};

C++ (like C) was designed to be implementable by a single-pass compiler. C ++(如C)旨在通过单程编译器实现。 Forward references are necessary in cases where the compiler needs to know that a symbol refers to a class before the class is actually defined. 如果编译器需要知道符号在实际定义类之前引用类,则前向引用是必需的。 The classic example of this is when two classes need to contain pointers to each other. 这个经典的例子是两个类需要包含彼此的指针。 ie

class B;

class A {
  B* b;
};

class B {
  A* a;
};

Without the forward reference to B, the compiler could not successfully parse the definition for A and you can't fix the problem by putting the definition of B before A. 如果没有对B的前向引用,编译器无法成功解析A的定义,并且您无法通过在A之前放置B的定义来解决问题。

In a language like C#, which needs a two-pass compiler, you don't need forward references 在像C#这样需要双向编译器的语言中,您不需要前向引用

class A {
  B b;
}

class B {
  A a;
}

because the compiler's first pass simply picks up all symbol definitions. 因为编译器的第一次传递只是选取所有符号定义。 When the compiler makes its second pass, it can say "I know B is a class because I saw the definition on my first pass". 当编译器进行第二次传递时,它可以说“我知道B是一个类,因为我在第一次传递时看到了这个定义”。

The compiler needs the definition of a class if member/methods of that class are accessed or if the size needs to be known. 如果访问该类的成员/方法或者需要知道大小,则编译器需要定义类。 In other cases, a forward declaration is sufficient. 在其他情况下,前瞻性声明就足够了。 This saves you compile time. 这节省了编译时间。 Example: 例:

class A { 
  B m_b; 
  C* m_ptrC;
}; 

For this class, you need the definition of B (size needed) and only the declaration of C (pointers have fixed size). 对于这个类,您需要定义B(需要的大小)并且只需要C的声明(指针具有固定大小)。 You only need to include the header of B, not the one of C. A forward declaration of C is sufficient. 您只需要包含B的标题,而不是C的标题.C的前向声明就足够了。

ah: 啊:

#ifndef A_H
#define A_H

#include <b.h>
class C;

class A
{
  B m_b;
  C* m_ptrC;
}
#endif

The forward declaration of c (instead of including ch which is also possible) saves you parsing ch whenever you include ah Across a large project, this may save a lot of compile time. c的前向声明(而不是包括ch也是可能的)可以节省你在每次包含时解析ch啊在大型项目中,这可以节省大量的编译时间。 Another benefit is, that changes in ch do not trigger a recompile for a in this case. 另一个好处是,在这种情况下,ch中的更改不会触发a的重新编译。 I do not know if compiler recognize this if you include ch instead of forward declaring it. 我不知道如果你包含ch而不是前面声明它,编译器是否会识别它。

For more information, try understanding the pimpl-idiom (just google for it. you'll get lots of hits). 有关更多信息,请尝试了解pimpl-idiom(只需google for it。您将获得大量点击)。

Of course - in a.cpp if you actually do something with the pointer to c (eg m_ptrC->Add()), you'll need to include ch But a.cpp is read only once where the header file is read n times with large n for classes that are used very often in large projects. 当然 - 在a.cpp中,如果你实际上用c指针做某事(例如m_ptrC-> Add()),你需要包含ch但a.cpp只读一次头文件被读n次对于在大型项目中经常使用的类,使用大n。

Forward declaration also allows for circular dependencies. 前向声明还允许循环依赖。 Example: 例:

class B;

class A {
 B* m_ptrB;
}

class B {
 A* m_ptrA;
}

Just remember - you can't use any information about size & methods if you use forward declarations. 请记住 - 如果使用前向声明,则不能使用有关大小和方法的任何信息。 This is also the case with 2 classes including each other (one class does not need the forward reference though). 这也是两个类相互包含的情况(尽管一个类不需要前向引用)。 I personally think circular references are bad style and you should avoid them if possible. 我个人认为循环引用是不好的风格,如果可能你应该避免它们。

For additional information: C++ FAQ 有关其他信息: C ++ FAQ

Thank you for the comment about the circular dependencies, I simply forgot them. 感谢您关于循环依赖关系的评论,我只是忘记了它们。

First, the class is declared , then it is defined . 首先, 声明类,然后定义它。

Declaration: It just tells the compiler: Ok, here's something (method, class, etc.), that can be used by the given name. 声明:它只是告诉编译器:好的,这是给定名称可以使用的东西(方法,类等)。 It just binds the given name to something. 它只是将给定的名称绑定到某个东西上。

Definition: It tells the compiler: Ok, here's what (and how) the methods, classes, etc. actually do their stuff. 定义:它告诉编译器:好的,这是方法,类等实际上做什么(以及如何)。 If something is defined, then the compiler is caused to actually allocate space for it. 如果定义了某些内容,则会使编译器为其实际分配空间。

You might have a look at here . 你可以看看这里

The first line is what's called a forward declaration. 第一行是所谓的前向声明。 It brings the name of the class into the current namespace without actually defining it. 它将类的名称带入当前命名空间而不实际定义它。

Ehm, the question is not very clear. 嗯,问题不是很清楚。 However, the code supplied declares a class test and defines it below, omitting the actual members (...). 但是,提供的代码声明了一个类test并在下面定义它,省略了实际的成员(...)。

The simple answer is that when you use the first form, you can then hold pointers or references to a class without needing the full implementation. 简单的答案是,当您使用第一个表单时,您可以保留指针或类的引用,而无需完整实现。 This can be very useful for when two classes tightly interact and their implementations or definitions are hard to separate. 当两个类紧密交互并且它们的实现或定义难以分离时,这非常有用。

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

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