Why separate variable definition and initialization in C++?

前端 未结 8 1512
心在旅途
心在旅途 2021-02-18 15:22

I\'m currently working on some quite old C++ code and often find things like

int i;
i = 42;

or

Object* someObject = NULL;
someO         


        
相关标签:
8条回答
  • 2021-02-18 16:12

    No. It's a matter of style.

    However if he ever wanted to move the declaration out of the function it would be less editing if the declaration and initialization is seperated.

    0 讨论(0)
  • 2021-02-18 16:17

    The change you propose is highly recommended! This is part of an important idiom in C++ programming, namely Resource Acquisition Is Initialization.

    0 讨论(0)
提交回复
热议问题