简体   繁体   中英

Pointer Member Variable Initialization in C++ Classes

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.

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?

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?

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.

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).

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 nullptr if needed), the second constructor will initialise it with the value passed (which isn't guaranteed to be good either!).

The value will be uninitialised so yes you do need to explicitly initialise it to 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.

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.

Value will be undefined.

You may have one "ultimate" ctor which will initialize all fields 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.

Even if the most voted answer is technically correct, I would suggest better approach is to initialise variable in class itself.

class A
{
};


class B
{
private:
    A* a_ = nullptr;

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

In C++, you are allowed to initialize values to member variables using the initialization list syntax. See this:

class AnyClass
{
};
class Xyz
{
    int n;
    AnyClass *p;
    Xyz() : n(55), p(new AnyClass())
    {
        // constructor body
    }
    ~Xyz() { delete p; }
};

The value of n becomes 55 and the pointer to the newly created AnyClass is initialized into p . Note that they are not assigned to n and p . They are initialized into the member variables.

When using new , you should delete it using delete When using new[] , you should use delete[] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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