C++ 类中的指针成员变量初始化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8406931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:31:21  来源:igfitidea点击:

Pointer Member Variable Initialization in C++ Classes

c++pointers

提问by John Fitzpatrick

This is going to sound so basic as to make one think I made zero effort to find the answer myself, but I swear I did search for about 20 minutes and found no answer.

这听起来非常基本,以至于让人们认为我自己找到答案的努力为零,但我发誓我确实搜索了大约 20 分钟,但没有找到答案。

If a private c++ class member variable (non-static) is a pointer, and it is NOT initialized in the constructor (either through an initialization list or an assignment in the constructor), what will its value be when the class is fully instantiated?

如果一个私有的 c++ 类成员变量(非静态)是一个指针,并且它没有在构造函数中初始化(通过初始化列表或构造函数中的赋值),那么当类完全实例化时它的值是什么?

Bonus question: If the answer to the above question is anything other than NULL, and I wish to always initialize a particular member pointer variable to NULL, and I have multiple constructors, do I really have to put an explicit initialization for that pointer in every constructor I write? And if so, how do the pros handle this? Surely nobody actually puts redundant initializers for the same member in all their constructors, do they?

附加问题:如果上述问题的答案不是 NULL,并且我希望始终将特定成员指针变量初始化为 NULL,并且我有多个构造函数,我是否真的必须在每个指针中显式初始化该指针?我写的构造函数?如果是这样,专业人士如何处理?确实没有人在所有构造函数中为同一个成员放置冗余初始化器,是吗?

EDIT:I wish I could've chosen two answers here. The smart pointers recommended by Bleep Bloop seem to be the elegantest approach, and it got the most up votes. But since I didn't actually use smart pointers in my work (yet), I chose the most illustrative answer that didn't use smart pointers as the answer.

编辑:我希望我能在这里选择两个答案。Bleep Bloop 推荐的智能指针似乎是最优雅的方法,它获得了最多的投票。但是由于我实际上并没有在我的工作中使用智能指针(还),所以我选择了没有使用智能指针的最具说明性的答案作为答案。

采纳答案by Moo-Juice

You're thinking correctly. If you don't initialise it, it could be anything.

你的想法是正确的。如果你不初始化它,它可以是任何东西。

So the answer to your question is yet, either initialise it with something, or give it a NULL (or nullptr, in the most recent C++ standard).

所以你的问题的答案是,要么用一些东西初始化它,要么给它一个 NULL (或 nullptr,在最新的 C++ 标准中)。

class A
{
};


class B
{
private:
    A* a_;

public:
    B() : a_(NULL) { };
    B(a* a) : a_(a) { };
};

Our default ctor here makes it NULL (replace with nullptrif needed), the second constructor will initialise it with the value passed (which isn't guaranteed to be good either!).

我们这里的默认nullptr构造函数使它为 NULL(如果需要,替换为),第二个构造函数将使用传递的值初始化它(这也不能保证是好的!)。

回答by Moo-Juice

The value will be uninitialised so yes you do need to explicitly initialise it to nullptr.

该值将未初始化,所以是的,您确实需要将其显式初始化为nullptr.

Using smart pointers (std::unique_ptr, std::shared_ptr, boost::shared_ptr, etc.) would mean that you don't need to do this explicitly.

使用智能指针(std::unique_ptrstd::shared_ptrboost::shared_ptr等)意味着您不需要显式执行此操作。

回答by Victor Sorokin

Value will be undefined.

值将是未定义的。

You may have one "ultimate" ctor which will initialize allfields and add "short-cut" ctors with only part of parameters, which will pass these params to ultimate ctor along with default values for the rest of params.

您可能有一个“最终”构造函数,它将初始化所有字段并添加仅具有部分参数的“快捷”构造函数,它将这些参数与其余参数的默认值一起传递给最终构造函数。

回答by johnathan

the value of any uninitialized pointer is always garbage, it's some random memory address.

任何未初始化的指针的值总是垃圾,它是一些随机内存地址。

in your constructors, you can use initializer lists to initialize your pointer

在您的构造函数中,您可以使用初始化列表来初始化您的指针

simply

简单地

MyClass::MyClass() : myPointer(nullptr)
{
}

trying to reference an uninitialized pointer triggers undefined behavior. so ALWAYS initialize your pointer.

尝试引用未初始化的指针会触发未定义的行为。所以总是初始化你的指针。