Should I explicitly zero initialize auto_ptr?
Asked Answered
H

3

5

Some of my colleagues prefer to explicitly initialize std::auto_ptr to 0 in constructor initialization list, but it will be initialized to 0 in it's constructor without any explicit initialization. So is there any reason to do it?

#include <memory>

class A
{
  A() : SomePtr(0)
  {
  }

private:
  std::auto_ptr<SomeType> SomePtr;
};
Heal answered 6/10, 2011 at 9:21 Comment(1)
possible duplicate of Is there any need to assign null pointer to std::auto_ptrErmina
B
9

No, the default constructor of std::auto_ptr does exactly that, so doing it explicitly is not necessary. In any case, it's a matter of style and you should be consistent. For instance, would you explicitly call the default constructor of a member vector in the constructor initialization list?

As a side note, std::auto_ptr is deprecated in the upcoming standard

Bait answered 6/10, 2011 at 9:23 Comment(2)
The "upcoming" standard has happened, it's now the "latest" standard! Hoorah, and cheering! Granted, it hasn't been implemented yet.Wonacott
Ok, the only reason is style and consistency.Heal
P
0

Psychology.

For built-in types, you probably already know they are uninitialized unless you do so explicitly. For classes, this is not the case.

A strive to consistency results in explicit initialization everywhere. This allows you to forget if A::SomePtr is a built-in or a class type. Pretty useless, imho, since the amount of built-in types is quite limited.

Photocell answered 6/10, 2011 at 9:27 Comment(0)
D
0

One reason maybe clarity, but that should be the only one. I myself prefer not to write unneccessary intialization, especially if that completely spares me from writing a default constructor for the surrounding class and just let the compiler do its job. Whereas it's merely a matter of style, I think too much over-paranoia does even harm the clarity of the code.

Driveway answered 6/10, 2011 at 9:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.