3

在 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
4

1 回答 1

1

是的,您可以在 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
于 2013-01-23T08:34:06.187 回答