【发布时间】:2019-04-15 09:01:11
【问题描述】:
我尝试以这种方式在类中使用“typedef struct”。 你能期待我想要的吗? 如您所见,这似乎是不可能的。 是不是真的不可能?
class CTestStructure
{
public:
typedef struct stTestInClass;
StTestInClass mmm; // Compile error C3646
};
struct CTestStructure::stTestInClass
{
int i;
}StTestInClass;
int main()
{
CTestStructure testStructure;
}
【问题讨论】:
-
typedef struct stTestInClass;在 C++ 中没有多大意义(命名为匿名struct)。struct stTestInClass;就足够了。虽然,StTestInClass mmm;使用不完整的struct作为成员。这行不通。 -
顺便说一句。另外,
typedef struct stTestInClass;中有一个错字:第一个s必须是大写。 -
感谢您的回复。但是,我想添加 'typedef' 的原因是允许其他类使用这种结构类型,例如 CTestStructure::StTestInClass aaa;
-
@SungsuKim 您不需要 typedef 就可以像 C++ 中那样使用 struct 标记作为类型名。
struct stTestInClass { int i; };然后CTestStructure::stTestInClass就可以了。
标签: c++