• ### Popular Now

• 10
• 9
• 13
• 10
• 18

#### Archived

This topic is now archived and is closed to further replies.

# declaring and initializing a variable

This topic is 5136 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have some questions about declaring and initializing variables in c and c++. I found a tutorial online that was explaining this concept, but presented something I had never seen before. They said to declare and initialize a variable in c was this... eg: int myVariable = 20; and in c++ to do this.... eg: int myVariable(20); I have some books ant have taken a class on c++, but had never heard of initializing the variable in that way( in() ). It also seems to me that you could only do that if you were delcaring and initializing the variable on the same line. Is that correct? Also..what is the industry standard on declaring and initializing variables? Personally I like declaring and initializing variables each on seperate lines. Thanks for you help.

##### Share on other sites
Sure you''ve heard of it before!

ifstream file("file.txt");

It''s just initialization by calling the constructor.

##### Share on other sites
You can do it both ways. i prefer the "=" sign, but whatever.

##### Share on other sites
For objects that are not just plain data, initializing in declaration is semantically different from assigning after construction.

Also, an "=" in a declaration turns into a constructor call, not an operator=() call.

Usually, you should initialize in declaration, not on some other line of code. This will generate better code for some classes, and also minimize the risk that the declaration and the initialization drift out of sync over time.

##### Share on other sites
your example of doing int myVariable(20); is just simply an alternative to your common way of doing it by int myVariable = 20; but that example makes its usage seem trivial. It most certainly serves a purpose that is part of class design in C++. If you haven't gotten to building any classes yet in C++ yet then all you need to know for now is that when initializing a variable/object you may be able to use various methods of constructing the object based on how the class was authored. For instance if you've used the standard template library you can declare a vector like this:

vector<int> x;vector<int> y(x); //make y an exact copy of x when constructing the object. vector<int> y(x.begin,x.end) /*this initialization of y is the same as the one before it in terms of the result, though each one has its own constructor called. Just like a function, you can sort of "overload" the constructors to take different parameters either iterators in this case, or another one that takes an object of the same type.*/

I hope it wasn't confusing. If you have a good grasp on vectors using stl, that example should help open up your thoughts a bit. If not, then well, I guess you can just take the whole initialization thing blindly and when you get to classes it will all make sense.

[edited by - nervo on February 27, 2004 1:45:29 AM]