Why separate variable definition and initialization in C++?

前端 未结 8 1509
心在旅途
心在旅途 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:05

    int i;
    i = 42;
    

    That is not separate variable definition and initialization.

    That is separate variable declaration and assignment. I don't see any reason for this. If you know the value at the time of declaration of variable, then initialize it right then. So your question doesn't has any rationale explanation.

    Of course, if you don't know the value at the time of declaration, then you don't have any choice, you need assignment then.

提交回复
热议问题