标签: forward-declaration

在C++中,是否可以将类声明为从另一个类继承?

我知道我能做到:

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++ inheritance forward-declaration

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

int main()是否需要在C ++上进行声明?

在阅读有关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

63
推荐指数
7
解决办法
7472
查看次数

使用unique_ptr进行前向声明?

我发现std::unique_ptr在下面的代码中结合使用前向声明类很有用.它编译并与GCC一起工作,但整个事情似乎有点奇怪,我想知道这是否是标准行为(即标准要求)?因为B声明时B不是完整的类型unique_ptr.

A.hpp

#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)

A.cpp

#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)中定义的.

c++ destructor forward-declaration unique-ptr

62
推荐指数
1
解决办法
3万
查看次数

什么是<iosfwd>标题?

<iosfwd>标题用于什么(在此文件中提到)?

为什么有必要?

任何例子?

c++ iostream forward-declaration iosfwd

59
推荐指数
3
解决办法
2万
查看次数

Objective-C:前瞻性宣言

我正在编写一个多RootViewController视图应用程序,它利用一个叫做在视图之间切换的类.

在我的MyAppDelegate标题中,我创建了一个被RootViewController调用的实例rootViewController.我已经看过这样的例子,其中@class指令被用作"前向类声明",但我不太确定这意味着什么或完成了什么.

#import <UIKit/UIKit.h>
@class RootViewController;
@interface MyAppDelegate
.
.
.
Run Code Online (Sandbox Code Playgroud)

objective-c forward-declaration c-preprocessor

47
推荐指数
1
解决办法
4万
查看次数

为什么C++好友类只需要在其他命名空间中使用前向声明?

假设我有一个类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)

c++ namespaces friend forward-declaration

46
推荐指数
2
解决办法
3万
查看次数

未命名结构的前向声明

赏金问题:所以,这两个Foo不是一回事.精细.第二种形式在图书馆中给出.鉴于我无法改变它,我该如何转发声明呢?


我一直以为C和C++允许重复声明,前提是没有重复的定义.然后我在尝试编写扩展C库的C++代码时遇到了这个问题.

struct Foo;
typedef struct {} Foo;
Run Code Online (Sandbox Code Playgroud)

这会出现以下错误:

'struct Foo'之前的声明为'struct Foo'

我想转发声明,嘲笑它!这有什么不对?

c c++ struct typedef forward-declaration

41
推荐指数
5
解决办法
2万
查看次数

错误:成员访问不完整类型:转发声明

我在同一个.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班.

你有什么想法解决我的问题吗?

最好的祝福.

c++ forward-declaration

37
推荐指数
2
解决办法
8万
查看次数

C++嵌套类/转发声明问题

是否可以转发声明一个嵌套类,然后将其用作外部类的具体(不是指向/引用)数据成员的类型?

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)

c++ nested-class forward-declaration

32
推荐指数
2
解决办法
3万
查看次数

为什么我不能在try块中定义之前使用Javascript函数?

作为讨论在这里,在定义前,他们可以用函数定义.但是只要一段代码被包装在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块显然有一些"特殊".有没有办法绕过这种行为?

javascript firefox function try-catch forward-declaration

32
推荐指数
2
解决办法
8631
查看次数