简体   繁体   English

我可以在 c++ 中键入未命名的结构/类吗?

[英]Can I typedef unnamed struct/class in c++?

In C, I can typedef unnamed (no tag) struct:在 C 中,我可以 typedef unnamed (no tag) 结构:

typedef struct {
 int val;
} Foo_t;

But when I tried to do the same in c++:但是当我尝试在 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() {}

output : output

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’

I know I am using constructor A(int a) of "unnamed" struct, but right after it, it is typedef ed.我知道我正在使用“未命名”结构的构造函数A(int a) ,但在它之后,它是typedef ed。 So constructors are only available to know types所以构造函数只能用于知道类型

The problem for example with this typedef declaration例如这个 typedef 声明的问题

typedef struct
{
    A(int a) : a_var(a) {}
    int a_var;
} A;

is that within the unnamed structure there is used undeclared name A as a name of a constructor.是在未命名的结构中使用未声明的名称 A 作为构造函数的名称。 So this declaration is invalid.所以这个声明是无效的。

By the way the same problem exists else in C.顺便说一句,C 中也存在同样的问题。

Consider for example a typedef declaration of a structure used to define a node of a linked list.例如,考虑用于定义链表节点的结构的 typedef 声明。

typedef struct
{
    int data;
    A *next;
} A;

Again the name A within the structure definition is undefined.同样,结构定义中的名称A是未定义的。

Even if you will write like即使你会这样写

typedef struct A
{
    int data;
    A *next;
} A;

nevertheless the name A is still undeclared within the structure in C.尽管如此,名称A仍未在 C 的结构中声明。 You have to write in C你必须写在 C

typedef struct A
{
    int data;
    struct A *next;
} A;

On the other hand, in C++ such a typedef declaration is valid.另一方面,在 C++ 中,这样的 typedef 声明是有效的。

Yes, you can, but it's not possible to make use of the type name inside the structure, as you said.是的,你可以,但正如你所说,在结构中使用类型名称是不可能的。

// this is valid
typedef struct{
   int x;
   void set(int n){x=n;}
} A;

// this is not
typedef struct{
    int x;
    B(int n){x = n;}
} B;

// you can only have constructor with named structs
typedef struct st_C{
    int x;
    st_C(int n){x = n;}
} C;

// this is valid
C *foo = new C(3); 

In C, you often have to write typdefs like this:在 C 中,您经常必须像这样编写 typdef:

typedef struct {
 int val;
} Foo_t;

to avoid having to write为了避免不得不写

struct Foo_t f;

every time, which soon becomes quite tedious.每次,这很快就会变得相当乏味。

In C++, all struct s union s and enum s declarations act as though they are implicitly typedef ed.在 C++ 中,所有struct union s 和enum s 声明的行为就像它们是隐式typedef一样。 So you can declare a struct like this:所以你可以像这样声明一个结构:

struct A
{
    A(int a) : a_var(a) {}
    int a_var;
};

However, structs in C++ are often aggregates (a simple collection of data), and you don't need the constructors.但是,C++ 中的结构通常是聚合(数据的简单集合),您不需要构造函数。 So the following is fine:所以以下是好的:

struct A
{
    int a_var;
    int b_var;
};

The constructor is implicitly defined by the compiler, and when you use value initialisation with braces:构造函数由编译器隐式定义,当您使用带有大括号的值初始化时:

A a{};

all the members of the struct will be zeroed out.结构的所有成员都将被清零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM