为什么编译器不让我向前声明类型定义?
假设这是不可能的,那么保持我的包含树小的最佳实践是什么?
为什么编译器不让我向前声明类型定义?
假设这是不可能的,那么保持我的包含树小的最佳实践是什么?
当前回答
对于那些像我一样,希望在一些c++代码中前向声明使用typedef定义的c风格结构体的人,我已经找到了一个解决方案,如下所示…
// a.h
typedef struct _bah {
int a;
int b;
} bah;
// b.h
struct _bah;
typedef _bah bah;
class foo {
foo(bah * b);
foo(bah b);
bah * mBah;
};
// b.cpp
#include "b.h"
#include "a.h"
foo::foo(bah * b) {
mBah = b;
}
foo::foo(bah b) {
mBah = &b;
}
其他回答
要“fwd声明一个类型定义”,你需要fwd声明一个类或结构,然后你可以typedef声明类型。编译器可以接受多个相同的类型。
长形式:
class MyClass;
typedef MyClass myclass_t;
简式:
typedef class MyClass myclass_t;
对于那些像我一样,希望在一些c++代码中前向声明使用typedef定义的c风格结构体的人,我已经找到了一个解决方案,如下所示…
// a.h
typedef struct _bah {
int a;
int b;
} bah;
// b.h
struct _bah;
typedef _bah bah;
class foo {
foo(bah * b);
foo(bah b);
bah * mBah;
};
// b.cpp
#include "b.h"
#include "a.h"
foo::foo(bah * b) {
mBah = b;
}
foo::foo(bah b) {
mBah = &b;
}
在c++(而不是普通C)中,对一个类型定义两次是完全合法的,只要两个定义完全相同:
// foo.h
struct A{};
typedef A *PA;
// bar.h
struct A; // forward declare A
typedef A *PA;
void func(PA x);
// baz.cc
#include "bar.h"
#include "foo.h"
// We've now included the definition for PA twice, but it's ok since they're the same
...
A x;
func(&x);
我也有同样的问题,不想在不同的文件中混淆多个typedef,所以我用继承解决了它:
was:
class BurstBoss {
public:
typedef std::pair<Ogre::ParticleSystem*, bool> ParticleSystem; // removed this with...
did:
class ParticleSystem : public std::pair<Ogre::ParticleSystem*, bool>
{
public:
ParticleSystem(Ogre::ParticleSystem* system, bool enabled) : std::pair<Ogre::ParticleSystem*, bool>(system, enabled) {
};
};
效果很好。当然,我必须改变所有的参考资料
BurstBoss::ParticleSystem
简单地
ParticleSystem
你可以使用正向类型定义。但是要做
typedef A B;
你必须先向前申报A:
class A;
typedef A B;