我在一个封闭类中有两个内部结构,在其中一个结构中,我有一个指向另一个结构类型的对象的指针成员。为了提高可读性并明确前向声明是针对需要它的结构的,我将前向声明放在内部结构本身中。像这样的东西
class Enclosing{
public:
struct InnerA{
struct InnerB; // forward declaration inside InnerA to improve readability
InnerB* b;
// other members
};
struct InnerB{
// lots of member variables
};
};
然后在外面的某个地方,我有一个函数
void DoSomething(){
Enclosing::InnerA a;
// error incompatible types Enclosing::InnerB* and Enclosing::InnerA::InnerB*
Enclosing::InnerB* ptr = a.b;
}
据我所知,正向声明只是一种告诉编译器类存在的方式,而不是定义一个完全不同的新类型。这是标准吗?如果是这样的话,有没有办法将正向声明放在结构中,而不被视为不同的类型?
https://stackoverflow.com/questions/56284127
复制相似问题