10

I wonder if I have a A* member in my class, shouldn't it we set to nullptr automatically if I have a constructor of my class in this form:

class MyCLass
{
    A* m_pointer;

public:
    MyCLass()
    {
    }
};

Does it matter if I do MyCLass* o = new MyCLass; or I do MyCLass* o = new MyCLass(); in C++11?

Narek
  • 38,779
  • 79
  • 233
  • 389
  • 3
    Can you please clarify your question? – juanchopanza Oct 01 '14 at 13:09
  • 2
    You don't need an explicit pointer in the first place, just use `std::unique_ptr`. – David G Oct 01 '14 at 13:11
  • 5
    @0x499602D2 What makes you think that this class should be the exclusive owner of the pointed to object? – James Kanze Oct 01 '14 at 13:15
  • @JamesKanze Unless there is evidence otherwise I would suggest he use that class. – David G Oct 01 '14 at 13:30
  • 1
    It _will_ be set to `nullptr` if you just mention it in the initializer_list, no exlicit value needed: `MyCLass() : m_pointer() { }` – MSalters Oct 01 '14 at 13:40
  • 1
    @0x499602D2 Why? It's very exceptional for an `std::unique_ptr` to be appropriate in a class. If the object lifetime should correspond exactly to that of the class, then just make the object a member, and don't use a pointer at all. – James Kanze Oct 01 '14 at 13:48
  • @JamesKanze My suggestion was a response to his question about *pointers* inside of a class. If he wants to use a *pointer* inside of a class then my first suggestion would be to use a `std::unique_ptr`. I don't know his situation to say it's better to use an actual `A` object, but that's not what the question's about. – David G Oct 01 '14 at 14:13
  • 1
    @0x499602D2 I don't know his actual situation either, but since the cases where `std::unique_ptr` would be appropriate are very rare, I certainly wouldn't recommend it. – James Kanze Oct 01 '14 at 14:34
  • @JamesKanze You wouldn't recommend `std::unique_ptr` in place of a raw pointer? Not sure I follow. – David G Oct 01 '14 at 14:43
  • 1
    @0x499602D2 Not in the general case. `std::unique_ptr` is very useful for certain specific cases, almost always as a local variable, but it certainly isn't appropriate for generic use. – James Kanze Oct 01 '14 at 15:06

2 Answers2

19

Pointers are "POD types"...a.k.a. "Plain Old Data". The rules for when and where they are default-initialized are summarized here:

Default initialization of POD types in C++

So no. It doesn't matter what your constructor for a class is, if it's a raw pointer as a member of the class. You aren't actually instantiating the class. So members like Foo * or std::vector<Foo> * or anything ending in * will not be initialized to nullptr.

The smart pointer classes are not POD. So if you use a unique_ptr<Foo> or a shared_ptr<Foo> that is creating instances of classes, that do have a constructor that makes them effectively null if you do not initialize them.

Does it matter if I do MyCLass* o = new MyCLass; or I do MyCLass* o = new MyCLass(); in C++11?

One question per question, please.

Do the parentheses after the type name make a difference with new?

Community
  • 1
  • 1
  • But if I don't define a constructor at all, I guess it will be initialized to `nullptr` by compiler generated default CTOR, right? – Narek Oct 01 '14 at 13:20
  • @Narek In that case you would need `MyClass m{};` or `MyCLass* m = new MyClass();` to ensure data members such as pointers are zero-initialized, i.e. you need to *value-initialize* the object. – juanchopanza Oct 01 '14 at 13:21
  • @Narek Nope. You could easily read with a search like "what all exactly does a default constructor do, and when is it invoked?" and find the likes of [this answer](http://stackoverflow.com/questions/2417065/does-the-default-constructor-initialize-built-in-types) – HostileFork says dont trust SE Oct 01 '14 at 13:21
3

The default constructor, if compiler-generated or defaulted, will default-initialize all members. Any user-defined constructor will similarly default-initialize all members that don't have an explicit initializer in the initializer-list.

To default-initialize means "call the default constructor for classes, leave everything else uninitialized".

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157