简体   繁体   English

为什么我们可以使用前向声明的 function 但不能实例化前向声明的 class?

[英]Why can we use a forward declared function but not instantiate a forward declared class?

While reading this post I was wondering why calling a function previously forward-declarated is possible like in this example在阅读这篇文章时,我想知道为什么可以像在这个例子中那样调用之前前向声明的 function

int f(int x, int y); // forward declaration


int main()
{
    return f(2,3);
}

int f(int x, int y)
{
    return x + y;
}

but instantiating or calling a member variable of a class previously forward-declared is not possible like in the example但是实例化或调用之前前向声明的 class 的成员变量是不可能的,就像在示例中一样

class Foo; // forward declaration

int main()
{
    Foo foo;
    return 0;
}

class Foo
{
    int x = 3;
};

My thaughts are that a function is just like an address, which is defined when the forward declaration is done (for example f() is assigned 0xABC ).我的想法是 function 就像一个地址,它是在前向声明完成时定义的(例如 f() 被分配0xABC )。 Then when parsing the return f(x,y) line the compiler just injects that address 0xABC in the binary code and then later when parsing the declaration of the function the compiler start inputting the instruction starting from address 0xABC .然后在解析return f(x,y)行时,编译器只在二进制代码中注入地址0xABC ,然后在解析 function 的声明时,编译器开始输入从地址0xABC开始的指令。

However, when parsing the class Foo forward declaration if the compiler assigns to that class to some memory say 0xDEF , then when it parses Foo foo it will not know how much space to allocate since all the members of the class were not defined yet so the compiler doesn't know how much space to allocate in total However, when parsing the class Foo forward declaration if the compiler assigns to that class to some memory say 0xDEF , then when it parses Foo foo it will not know how much space to allocate since all the members of the class were not defined yet so the编译器不知道总共要分配多少空间

I don't really know anything about compilers, so is this correct?我对编译器一无所知,所以这是正确的吗?

Your assumptions are mostly correct, but you don't need to think about how compilers work.你的假设大多是正确的,但你不需要考虑编译器是如何工作的。

By forward declaring a function, you make its signature available to the rest of the code.通过前向声明 function,您可以使其签名可用于代码的 rest。 This means its name, return value, and parameter types are known, and that's all you need syntactically to call that function.这意味着它的名称、返回值和参数类型是已知的,这就是您在语法上调用 function 所需的全部内容。

By forward declaring a class, you only make it known that its name refers to a type.通过前向声明一个 class,你只会让它知道它的名字是指一个类型。 That enables you to use it in various contexts.这使您可以在各种情况下使用它。 For example, you can declare (but not define) a function with that class as a parameter type or as the return type.例如,您可以声明(但不定义)一个 function ,该 class 作为参数类型或返回类型。 Or you can define a pointer to that class, because the type a pointer points to doesn't matter as long as you don't try to dereference it.或者您可以定义一个指向该 class 的指针,因为只要您不尝试取消引用它,指针指向的类型并不重要。 Or you can use it as argument to a typename in a template, as long as that template doesn't actually use it (but it could define a pointer to it or declare a function with it as parameter type and so on).或者,您可以将其用作模板中类型名的参数,只要该模板实际上并未使用它(但它可以定义指向它的指针或声明 function 并将其用作参数类型等等)。

Knowing only that some particular name refers to a class, you cannot deduce anything about its contents (and yes, its size) or how to actually use it.仅知道某个特定名称是指 class,您无法推断出有关其内容(是的,其大小)或如何实际使用它的任何信息。

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

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