默认初始化整型成员是否首选空初始值设定项?

发布于 2024-09-08 05:52:02 字数 648 浏览 6 评论 0原文

我刚刚读了 GMan 的评论,

class A
{
public:
   A() :
      m_ptr() // m_ptr is implicitly initialized to NULL
   { }
};

应该优先于

class A
{
public:
   A() :
      m_ptr(NULL) // m_ptr is explicitly initialized to NULL
   { }
};

注意第一个示例中缺少 NULL

G曼说得对吗?这可能有点主观,因此“您是否更喜欢默认初始化的空初始化器?”可能更合适。

另外,如果您更喜欢空初始值设定项,这是否适用于其他整数成员?

class B
{
public:
   B() :
      m_count(),
      m_elapsed_secs()
   {}
private:
   std::size_t m_count;
   float m_elapsed_secs;  //the elapsed time since instantiation
};

当然,请捍卫您的观点,并说明为什么其中一种应该优于另一种。

I just read a comment by GMan that

class A
{
public:
   A() :
      m_ptr() // m_ptr is implicitly initialized to NULL
   { }
};

should be preferred over

class A
{
public:
   A() :
      m_ptr(NULL) // m_ptr is explicitly initialized to NULL
   { }
};

Notice the lack of NULL in the first example.

Is GMan right? This might kinda subjective, so "Do you prefer empty initializers for default initialization?" might be more appropriate.

Also if you prefer empty initializers, do does this apply to other integral members?

class B
{
public:
   B() :
      m_count(),
      m_elapsed_secs()
   {}
private:
   std::size_t m_count;
   float m_elapsed_secs;  //the elapsed time since instantiation
};

Of course, please defend your view point with a description of why one should be preferred over the other.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤芳又自赏 2024-09-15 05:52:02

我更喜欢明确的。正如这个问题的一些错误答案所表明的那样,并不是每个人都清楚地知道 int()int(0) 是等价的。

我认为不提供显式值的优点是,如果您更改类型,则无需重新访问初始化列表。

I prefer the explicitness. As some of the wrong answers to this question have demonstrated, it's not obvious to everyone that, say, int() and int(0) are equivalent.

I suppose not supplying an explicit value has the advantage that you won't need to revisit the initialization list if you ever change the type.

爱的十字路口 2024-09-15 05:52:02

首先,我说它可以说更好,但事实并非如此。 :) 另外,更多的是要摆脱 NULL;我只是碰巧什么也没用,而不是 0。但无论如何,这是一个有趣的问题。

这可能只是风格问题,但值得注意的是, 正如约翰内斯所做的,这不仅仅是句法风格;而是。他们做不同的事情。将这些不同的东西变得相同很容易。

我更喜欢值初始化,因为我不参与值初始化的任何部分;我只是说“被初始化”。相反,一旦引入一个值,您就会影响该值的初始化方式。

我认为您很难找到一种价值初始化明显更好的情况,只需选择哪一个更适合您即可。

Firstly, I said it's arguably better, not that it is. :) Also, it was more about getting rid of NULL; I just happen to use nothing instead of 0. But an interesting question anyway.

It's probably just a matter of style, but it's important to note, as Johannes did, that it's not just syntactical style; they do different things. It's just easy to make those different things the same.

I prefer value-initialization, because I'm not taking any part of how the value is being initialized; I'm simply saying "be initialized." Contrarily, once you introduce a value you are influencing how the value is initialized.

I think you'd be hard-pressed to find a situation where value-initialization is clearly better, just pick which one suits you more.

无悔心 2024-09-15 05:52:02

当您编写模板类来默认初始化依赖类型的成员时,默认初始化是必需的。对于其他情况,如果您想默认初始化成员,则没有真正的区别。但在某些情况下,您想要获得非默认行为。

默认初始化不合适时的一个示例:

struct X {
  HANDLE some_file_handle;
  // note that INVALID_HANDLE_VALUE is not equal to 0 in Windows
  X() : some_file_handle( INVALID_HANDLE_VALUE ) {} 
};

至于使用 NULL 与默认初始化,我还有一个例子:在 Visual Studio 2010 中,它被声明为在某种程度上与 C++0x NULL< 一致/code> 仍然定义为 0,但在 C++0x 中,您应该使用 nullptr 来初始化指针。 C++'03 中未定义 nullptr。但您可能想让您的代码可移植。在这种情况下(初始化指针)我会更喜欢默认初始化而不是值初始化。

Default initialization is necessary when you write a template class to default initialize members of dependent types. For other cases there're no real difference if you want to default initialize the member. But there are some cases when you want to get non default behavior.

One sample when default initialization is not suitable:

struct X {
  HANDLE some_file_handle;
  // note that INVALID_HANDLE_VALUE is not equal to 0 in Windows
  X() : some_file_handle( INVALID_HANDLE_VALUE ) {} 
};

As for using NULL vs default initialization I have one more example: in Visual Studio 2010 which is declared to be somehow conformant with C++0x NULL is still defined as 0, but in C++0x you should use nullptr to initialize pointers. And nullptr is not defined in C++'03. But you probably want to make your code portable. In that case (to initialize pointers) I will prefer a default initialization over value initialization.

つ低調成傷 2024-09-15 05:52:02

我认为它们是不同的东西,您是否可能将 NULLvoid 混淆,这意味着 int main()int 相同main(void) 但不是 int main(NULL) (在 C++ 中 NULL 相当于 0

I think they are different things, is it possible you are confusing NULL with void meaning int main() is the same as int main(void) but NOT int main(NULL) (In C++ NULL is equivalent to 0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文