No need to initialize string (i.e. string Guess = "";)

(I code in C++ professionally, but am watching the videos with my kids)

String variables are already initialized to an empty string, so the suggested initialization in lecture 17 is a no-op.

In some ways I guess it’s good practice (as a type like ‘int’ will no be so initialized and if the read operation on cin fails, an int variable will continue to be uninitialized). But overall I would suggest that failing to understand that most STL types are initialized to sensible ‘empty’ values is bad practice.

I mentioned this to @ben right at the beginning when this course started. He stated that he wants to reinforce the importants of initalising your variables.
Though I agree with your assesment, I’m kind of torn since the subject since class’ and constructors comes much later IIRC.

Being a current programmer at my company I’ve had to deal with enough bugs to say this: "Don’t assume…"
Not all compilers are built equally(read windows) to do proper initialization at all. Honestly, when making a course, I think @ben is on the money when he enforces the rule of explicit initialization. It’s just safer cross-platform(compiler)

1 Like

In over 30 years of programming, I have seen more compilers than I would care to count that do NOT initialize variables. Yes, it’s true that all the programming environments I’m currently using do initialize, but I simply will not rely on that fact. During my first programming class in High School, the teacher had a great big sign at the top of the blackboard that read:

 Assume Nothing

It’s a lesson I’ve stuck to ever since.

1 Like

It’s not a cross-platform or environment issue. C++ strings are not sometimes initialized by the compiler and sometimes not. This is part of the definition of C++ string class. Similarly I would be surprised that a local-scoped or dynamically allocated block of memory ever was initialized if it didn’t have a constructor (and, frankly, would be outraged if a run-time wasted its time zeroing memory on my behalf).

I would never suggest depending on the initialization of something that doesn’t have a defined value at start, of course, and given that the student hasn’t yet seen any example of another variable type at this stage I suppose it’s a reasonable way of being consistent (rather than trying to explain to them the different behaviors of, say, string vs int).

@Geoff_Langdale, your double negative language confused me at first and thought you were saying the opposite :stuck_out_tongue: .

Though this whole topic goes into class design, it’s not an unreasonable assumption to make that a default constructor would initialise it’s members since that’s kind of the whole point of one. If your class’ default constructor doesn’t initialise it’s members you should tell your users that since one would expect it to.
Which is exactly what Epic does:

/** Default constructor (no initialization). */

P.S. if your std::string's default constructor doesn’t construct an empty string you should throw whatever implementation of the standard library you are using in a fire since it’s outrageously not compliant with the standard.

@DanM absolutely agree, especially on the PS. I’ve spent many years thinking about this exact issue (sometimes in C rather than C++, where it’s all a bit less language-supported). We ( wrote a data structure that was designed to deal with the problem of having a bitfield with 50K members that might only be alive for about 3 or 4 write operations (think “setting a bit”) before being discarded. So we’ve spent a lot of time where it’s absolutely peformance-critical to not just blindly initialize a bunch of stuff to be on the safe side.

Maybe not the biggest priority to be thinking about when introducing newbies to C++. :slight_smile:

Privacy & Terms