结构成员的默认值 [英] Default value of a struct member

查看:60
本文介绍了结构成员的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我确定这个问题已经得到回答,我只是不确定使用正确的词来提问.如果有人告诉我正确的术语是什么,那真是太棒了!)

我正在C ++中为数据结构类实现 HashSet ,并且我对有关结构的C ++语法有疑问.这是我的代码:

I'm implementing a HashSet in C++ for a data structures class, and I have a question about C++ syntax regarding structs. Here is my code:

struct HashNode
{
    T value;
    HashNode* next = nullptr;
};

在调用 new HashNode 时,此代码是否可以正确地初始化指向 nullptr next 指针?如果不是,那么在 new HashNode 之后的 next 的值是什么?

Will this code correctly initialize the next pointer to nullptr when new HashNode is called? If not, what is the value of next after new HashNode?

推荐答案

在调用新的HashNode时,此代码是否可以正确初始化下一个指向nullptr的指针?

Will this code correctly initialize the next pointer to nullptr when new HashNode is called?

是的,它将被初始化为 nullptr .这是类中的花括号或相等的初始化程序(默认成员初始化程序)(从C ++ 11开始),如果在成员初始化程序列表中省略了该成员,则将使用它.

Yes, it will be initialized to nullptr. This is in-class brace-or-equal initializer (default member initializer) (since C++11), it will be used if the member is omitted in member initializer list.

通过默认的成员初始化程序,它只是一个大括号或等于成员声明中包含的初始值设定项,用于如果成员初始化程序列表中省略了该成员

Through a default member initializer, which is simply a brace or equals initializer included in the member declaration, which is used if the member is omitted in the member initializer list

如果成员具有默认的成员初始值设定项,并且也出现在构造函数中的成员初始化列表,默认成员初始化程序将被忽略.

If a member has a default member initializer and also appears in the member initialization list in a constructor, the default member initializer is ignored.

这篇关于结构成员的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆