相关疑难解决方法(0)

什么是C++中的前向声明?

在:http://www.learncpp.com/cpp-tutorial/19-header-files/

提到以下内容:

add.cpp:

int add(int x, int y)
{
    return x + y;
}
Run Code Online (Sandbox Code Playgroud)

main.cpp中:

#include <iostream>

int add(int x, int y); // forward declaration using function prototype

int main()
{
    using namespace std;
    cout << "The sum of 3 and 4 is " << add(3, 4) << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我们使用了前向声明,以便编译器在编译时知道" add"是什么main.cpp.如前所述,为要在其他文件中使用的每个函数编写前向声明可能会很快变得乏味.

你能进一步解释" 前瞻性宣言 "吗?如果我们在main()函数中使用它会有什么问题?

c++ declaration forward-declaration

202
推荐指数
4
解决办法
25万
查看次数

在C++中转发嵌套类型/类的声明

我最近遇到了这样的情况:

class A
{
public:
    typedef struct/class {...} B;
...
    C::D *someField;
}

class C
{
public:
    typedef struct/class {...} D;
...
    A::B *someField;
}
Run Code Online (Sandbox Code Playgroud)

通常你可以声明一个类名:

class A;
Run Code Online (Sandbox Code Playgroud)

但是你不能转发声明一个嵌套类型,以下导致编译错误.

class C::D;
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

c++ nested class forward-declaration

189
推荐指数
3
解决办法
5万
查看次数

标签 统计

c++ ×2

forward-declaration ×2

class ×1

declaration ×1

nested ×1