在 C++ 中这样做是否合法/建议
//Interface.h
#ifndef INTERFACE_H
#define INTERFACE_H
#include "WinImplementation.h"
#include "NixImplementation.h"
class Interface {
class WinImplementation;
class NixImplementation;
}
#endif
//WinImplementation.h
#ifndef WINIMPLEMENTATION_H
#define WINIMPLEMENTATION_H
#include "Interface.h"
class Interface::WinImplementation {}
#endif
//NixImplementation.h
#ifndef NIXIMPLEMENTATION_H
#define NIXIMPLEMENTATION_H
#include "Interface.h"
class Interface::NixImplementation {}
#endif
最佳答案
是的,您可以在 C++ 中前向声明嵌套类。以下示例直接取自 C++ 标准(第 9.7.3 节):
class E
{
class I1; // forward declaration of nested class
class I2;
class I1 {}; // definition of nested class
};
class E::I2 {}; // definition of nested class
关于C++ 在一个文件中定义类并在另一个文件中转发声明它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14474905/