0

Just trying to clarify my thoughts here. Here's my current understanding.

part 1:
Declaration vs initialization...

My understanding is that you can declare a variable without giving it a value and doing so is called declaring an uninitialized variable?
eg.

int var;

Declares int var without initializing it. Correct?

Whereas declaring a variable with a value we would say we're initializing this variable with the value {v}...

int var = 0;
  • Yes? No? Something totally different?

part 2:
Also if you're not going to give it a value why declare it at all? I see this all the time but I don't know if there's a reason to do it this way as opposed to assigning a value when you create it. Is it idiomatic or aesthetic preference or is there a reason? If there's a reason, should you ALWAYS do it this way? What's the time and the place and limitations?
eg.

I often see this...

int var;
var = 0;

Why not just do this?...

int var = 0;

Thanks in advance :)

CWW
  • 21
  • 5
  • 1
    What source are you using to learn C++? What does it say about this topic? – Karl Knechtel Mar 22 '21 at 00:03
  • I'm drawing from a number of sources none of them give an explicit opinion they just assume one approach or the other and it often seems inconsistent. I'm wondering if the declaration before assignment approach is meant to demonstrate that it's possible or that it's preferred, if there's a reason to prefer one or the other it's never been made clear to me. – CWW Mar 22 '21 at 00:11
  • @KarlKnechtel On a related note I'm about 30 hours into studying, this isn't my first programming language but I've mostly worked in higher level interpreted languages, I sorta got into rust a number of years ago but I really wanna dive deep with c++. If you know of good resources I'm open to suggestions. :) – CWW Mar 22 '21 at 00:13
  • 1
    You can start here https://stackoverflow.com/questions/388242 – cigien Mar 22 '21 at 00:19
  • @cigien Excellant resource, thank you! :D – CWW Mar 22 '21 at 00:29

0 Answers0