gpt4 book ai didi

c++ - 结构成员的默认值

转载 作者:搜寻专家 更新时间:2023-10-31 00:57:29 24 4
gpt4 key购买 nike

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

我正在用 C++ 为数据结构类实现一个 HashSet,我有一个关于结构的 C++ 语法的问题。这是我的代码:

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

new HashNode 被调用时,这段代码能正确地将 next 指针初始化为 nullptr 吗?如果不是,new HashNode 之后的next 的值是多少?

最佳答案

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

是的,它会被初始化为nullptr。这是 in-class brace-or-equal initializer (default member initializer) (C++11 起),如果成员在成员初始化器列表中省略,则使用它。

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.

关于c++ - 结构成员的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37369924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com