我知道我能做到:
class Foo;
Run Code Online (Sandbox Code Playgroud)
但是我可以将一个类声明为继承自另一个类,例如:
class Bar {};
class Foo: public Bar;
Run Code Online (Sandbox Code Playgroud)
示例用例是共变量引用返回类型.
// somewhere.h
class RA {}
class RB : public RA {}
Run Code Online (Sandbox Code Playgroud)
...然后在另一个不包含somewhere.h的标题中
// other.h
class RA;
class A {
public:
virtual RA* Foo(); // this only needs the forward deceleration
}
class RB : public RA; // invalid but...
class B {
public:
virtual RB* Foo(); //
}
Run Code Online (Sandbox Code Playgroud)
唯一的信息编译器应该需要处理的声明,RB* B:Foo()
即RB
具有RA
作为公共基类.现在显然你需要somewhere.h如果你打算从中做任何类型的反引用返回值Foo
.但是,如果某些客户端从不调用Foo
,那么它们没有理由包含somewhere.h,这可能会显着加快编译速度.
在阅读有关C ++中的函数的知识时,我被告知函数需要调用声明。例如:
#include <iostream>
int main() {
std::cout << "The result is " << sum(1, 2);
return 0;
}
int sum(int x, int y) {
return x + y;
}
Run Code Online (Sandbox Code Playgroud)
由于该函数没有声明,因此返回错误sum
。
main.cpp:4:36: error: use of undeclared identifier 'sum'
std::cout << "The result is " << sum(1, 2);
^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
为了解决这个问题,我将添加声明:
#include <iostream>
int sum(int x, int y); // declaration
int main() {
std::cout << "The result is " << sum(1, 2);
return 0;
}
int sum(int …
Run Code Online (Sandbox Code Playgroud) c++ program-entry-point forward-declaration function-declaration
我发现std::unique_ptr
在下面的代码中结合使用前向声明类很有用.它编译并与GCC一起工作,但整个事情似乎有点奇怪,我想知道这是否是标准行为(即标准要求)?因为B声明时B不是完整的类型unique_ptr
.
#include <memory>
class B;
class A {
std::unique_ptr<B> myptr;
// B::~B() can't be seen from here
public:
~A();
};
Run Code Online (Sandbox Code Playgroud)
#include "B.hpp"
//B.hpp has to be included, otherwise it doesn't work.
A::~A() = default; // without this line, it won't compile
// however, any destructor definiton will do.
Run Code Online (Sandbox Code Playgroud)
我怀疑这与析构函数有关(因此需要调用析构函数unique_ptr<B>
)是在特定的编译单元(A.cpp)中定义的.
我正在编写一个多RootViewController
视图应用程序,它利用一个叫做在视图之间切换的类.
在我的MyAppDelegate
标题中,我创建了一个被RootViewController
调用的实例rootViewController
.我已经看过这样的例子,其中@class指令被用作"前向类声明",但我不太确定这意味着什么或完成了什么.
#import <UIKit/UIKit.h>
@class RootViewController;
@interface MyAppDelegate
.
.
.
Run Code Online (Sandbox Code Playgroud) 假设我有一个类F
应该是类G
(在全局命名空间中)和C
(在命名空间中A
)的朋友.
A::C
,F
必须向前宣布.G
,没有F
必要的前瞻性声明.A::BF
可以成为朋友而A::C
无需前瞻性声明下面的代码说明了这一点,并使用GCC 4.5,VC++ 10以及至少与另一个编译器进行编译.
class G {
friend class F;
int g;
};
// without this forward declaration, F can't be friend to A::C
class F;
namespace A {
class C {
friend class ::F;
friend class BF;
int c;
};
class BF {
public:
BF() { c.c = 2; }
private:
C c;
};
} // …
Run Code Online (Sandbox Code Playgroud) 赏金问题:所以,这两个Foo
不是一回事.精细.第二种形式在图书馆中给出.鉴于我无法改变它,我该如何转发声明呢?
我一直以为C和C++允许重复声明,前提是没有重复的定义.然后我在尝试编写扩展C库的C++代码时遇到了这个问题.
struct Foo;
typedef struct {} Foo;
Run Code Online (Sandbox Code Playgroud)
这会出现以下错误:
'struct Foo'之前的声明为'struct Foo'
我想转发声明,嘲笑它!这有什么不对?
我在同一个.cpp上有两个类:
// forward
class B;
class A {
void doSomething(B * b) {
b->add();
}
};
class B {
void add() {
...
}
};
Run Code Online (Sandbox Code Playgroud)
前进不起作用,我无法编译.
我有这个错误:
error: member access into incomplete type 'B'
note: forward declaration of 'B'
Run Code Online (Sandbox Code Playgroud)
我正在使用clang编译器(clang-500.2.79).
我不想使用多个文件(.cpp和.hh),我想在一个.cpp上编码.
我不能在A班之前写B班.
你有什么想法解决我的问题吗?
最好的祝福.
是否可以转发声明一个嵌套类,然后将其用作外部类的具体(不是指向/引用)数据成员的类型?
IE
class Outer;
class Outer::MaybeThisWay // Error: Outer is undefined
{
};
class Outer
{
MaybeThisWay x;
class MaybeThatOtherWay;
MaybeThatOtherWay y; // Error: MaybeThatOtherWay is undefined
};
Run Code Online (Sandbox Code Playgroud) 作为讨论在这里,在定义前,他们可以用函数定义.但是只要一段代码被包装在try块中,就不再是这种情况了.
这显示"Hello world":
hello();
function hello() { alert("Hello world"); }
Run Code Online (Sandbox Code Playgroud)
但是这会显示"ReferenceError:hello is not defined":
try {
hello();
function hello() { alert("Hello world"); }
} catch (err) {
alert(err);
}
Run Code Online (Sandbox Code Playgroud)
因此,关于函数声明的try块显然有一些"特殊".有没有办法绕过这种行为?
c++ ×8
c ×1
destructor ×1
firefox ×1
friend ×1
function ×1
inheritance ×1
iosfwd ×1
iostream ×1
javascript ×1
namespaces ×1
nested-class ×1
objective-c ×1
struct ×1
try-catch ×1
typedef ×1
unique-ptr ×1