我正在寻找允许在另一个类的头文件中进行类的前向声明的定义:
我是否允许为基类,作为成员持有的类,通过引用传递给成员函数的类等执行此操作?
编译器为什么不让我转发声明一个typedef?
假设这是不可能的,保持包含树小的最佳做法是什么?
在: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()
函数中使用它会有什么问题?
在我的iOS5应用程序中,我有NSObject
States
类,并尝试初始化它:
states = [states init];
Run Code Online (Sandbox Code Playgroud)
这里的init
方法是States
:
- (id) init
{
if ((self = [super init]))
{
pickedGlasses = 0;
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
但是线路上有错误 states = [states init];
接收器类型"States"例如message是一个前向声明
这是什么意思?我究竟做错了什么?
我最近遇到了这样的情况:
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)
有任何想法吗?
是否可以在Python中转发声明一个函数?我想cmp
在声明之前使用我自己的函数对列表进行排序.
print "\n".join([str(bla) for bla in sorted(mylist, cmp = cmp_configs)])
Run Code Online (Sandbox Code Playgroud)
我已经组织了我的代码来cmp_configs
在调用之后放置方法的定义.它失败并出现此错误:
NameError: name 'cmp_configs' is not defined
Run Code Online (Sandbox Code Playgroud)
cmp_configs
在使用之前有没有办法"声明" 方法?这会让我的代码看起来更干净吗?
我假设有些人会试图告诉我,我应该重新组织我的代码,以便我没有这个问题.但是,有些情况下这可能是不可避免的,例如在实现某种形式的递归时.如果您不喜欢这个例子,假设我有一个案例,其中确实需要转发声明一个函数.
考虑这种情况,在Python中需要向前声明一个函数:
def spam():
if end_condition():
return end_result()
else:
return eggs()
def eggs():
if end_condition():
return end_result()
else:
return spam()
Run Code Online (Sandbox Code Playgroud)
凡end_condition
与end_result
先前已经被定义.
是重新组织代码并始终在调用之前放置定义的唯一解决方案吗?
可能重复:
在C++中转发嵌套类型/类的声明
我有一个这样的课......
class Container {
public:
class Iterator {
...
};
...
};
Run Code Online (Sandbox Code Playgroud)
在其他地方,我想通过引用传递一个Container :: Iterator,但我不想包含头文件.如果我尝试转发声明类,我会遇到编译错误.
class Container::Iterator;
class Foo {
void Read(Container::Iterator& it);
};
Run Code Online (Sandbox Code Playgroud)
编译上面的代码给出了......
test.h:3: error: ‘Iterator’ in class ‘Container’ does not name a type
test.h:5: error: variable or field ‘Foo’ declared void
test.h:5: error: incomplete type ‘Container’ used in nested name specifier
test.h:5: error: ‘it’ was not declared in this scope
Run Code Online (Sandbox Code Playgroud)
我怎样才能转发声明这个类,所以我不必包含声明Iterator类的头文件?
例如bash
,在C/C++中众所周知的前向声明是否有类似的东西或至少类似的东西(解决方法)?
或者有这样的事情,因为例如它总是一次性执行(一行一行)?
如果没有前向声明,我该怎么做才能使我的脚本更容易阅读.它相当长,这些函数定义在开始时与全局变量混合,使我的脚本看起来很难看,难以阅读/理解)?我要求学习一些这些案例的知名/最佳实践.
例如:
# something like forward declaration
function func
# execution of the function
func
# definition of func
function func
{
echo 123
}
Run Code Online (Sandbox Code Playgroud) 给出如下的模板类:
template<typename Type, typename IDType=typename Type::IDType>
class Mappings
{
public:
...
Type valueFor(const IDType& id) { // return value }
...
};
Run Code Online (Sandbox Code Playgroud)
有人如何在头文件中声明此类?
每当类声明仅使用另一个类作为指针时,使用类前向声明而不是包含头文件是否有意义,以便先发制人地避免循环依赖的问题?所以,而不是:
//file C.h
#include "A.h"
#include "B.h"
class C{
A* a;
B b;
...
};
Run Code Online (Sandbox Code Playgroud)
改为:
//file C.h
#include "B.h"
class A;
class C{
A* a;
B b;
...
};
//file C.cpp
#include "C.h"
#include "A.h"
...
Run Code Online (Sandbox Code Playgroud)
有什么理由不尽可能不这样做吗?