前瞻性声明的目的是什么? [英] What is the purpose of forward declaration?
问题描述
这是什么的描述或意义:
例如:
what is the description or meaning of this: for example:
class test;
class test
{
.....
};
推荐答案
C ++一个单程编译器。在编译器需要知道一个符号引用实际定义类之前的类的情况下,转发引用是必要的。这种情况的典型例子是两个类需要包含彼此的指针。即
C++ (like C) was designed to be implementable by a single-pass compiler. 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. i.e.
class B;
class A {
B* b;
};
class B {
A* a;
};
如果没有向前引用B,编译器无法成功解析A的定义, t通过在A之前放置B的定义来修复问题。
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.
在像C#这样需要两遍编译器的语言中,不需要前向引用
In a language like C#, which needs a two-pass compiler, you don't need forward references
class A {
B b;
}
class B {
A a;
}
因为编译器的第一遍只是拾取所有符号定义。当编译器第二次通过时,它可以说我知道B是一个类,因为我在第一次通过时看到了定义。
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".
这篇关于前瞻性声明的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!