Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why separate variable definition and initialization in C++?

Tags:

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

int i; i = 42; 

or

Object* someObject = NULL; someObject = new Object(); 

or even

Object someObject; someObject = getTheObject(); 

I completely understand what this code does but I really have no idea when such a separation of variable definition and initialization could be helpful. I searched for some explanations but always ended up with member initialization lists or the question when you should define your local variables.

In the end, I don't understand the reason why someone could have intentionally written this code. It just splits definition and initialization up into two subsequent lines and creates overhead – in the last case it creates an object using the default constructor only to destroy it in the next line.

I wonder whether I should simply change the code to

int i = 42; Object* someObject = new Object(); Object someObject = getTheObject(); 

Could this lead to any problems?

like image 412
Baldewin Avatar asked Mar 23 '11 10:03

Baldewin


People also ask

What is the need of declaration and initialization of a variable?

Declaration tells the compiler about the existence of an entity in the program and its location. When you declare a variable, you should also initialize it. Initialization is the process of assigning a value to the Variable. Every programming language has its own method of initializing the variable.

What is the difference between initialization and declaration of a variable?

Declarations specify the type followed by the name, and optionally the initialization. Initialization sets the variable to a new instance. It must be to a type that is compatible with the declaration type. In the above code, the variable a is declared as a string and is initialized to "Hello World".

What is difference between Definition & initialization?

For a variable, a definition is a declaration which allocates storage for that variable. Initialization is the specification of the initial value to be stored in an object, which is not necessarily the same as the first time you explicitly assign a value to it.

Why should we initialize a variable in C program?

If the variable is in the scope of of a function and not a member of a class I always initialize it because otherwise you will get warnings. Even if this variable will be used later I prefer to assign it on declaration. As for member variables, you should initialize them in the constructor of your class.


1 Answers

Object someObject; someObject = getTheObject(); 

This uses the assignment operator.

Object someObject = getTheObject(); 

This uses the copy constructor.

Apart from that, your suggested changes are equivalent, and you should implement them. The copy ctor/assignment operator difference is expected to produce the same result, this is not enforced by the language though.

I see no valid reason to split up declaration and assignment like the original code does - even though for all practical purposes it doesn't introduce overhead (except for the object)

like image 136
Erik Avatar answered Sep 29 '22 09:09

Erik