【发布时间】:2020-08-15 10:47:40
【问题描述】:
在 C 中,我可以 typedef unnamed (no tag) struct:
typedef struct {
int val;
} Foo_t;
但是当我尝试在 c++ 中做同样的事情时:
typedef struct
{
A(int a) : a_var(a) {}
int a_var;
} A;
typedef struct : public A
{
B(int a, int b) : A(a), b_var(b) {}
int b_var;
} B;
B &getB()
{
static B b(1, 2);
return b;
}
int main() {}
输出:
error: ISO C++ forbids declaration of ‘A’ with no type
error: only constructors take member initializers
error: class ‘<unnamed struct>’ does not have any field named ‘A’
我知道我正在使用“未命名”结构的构造函数A(int a),但紧随其后的是typedefed。所以构造函数只能用于知道类型
【问题讨论】:
-
您的标题问题与说明中隐含的问题不匹配。
标签: c++ struct language-lawyer typedef