Bool initialization in class

This is a discussion on Bool initialization in class within the C++ Programming forums, part of the General Programming Boards category; Hey!
I've got a class with a private bool isScanning, and I'm trying to set it to false when initializing ...

But isn't it being initialized when it goes to compile the private class variables? Or does that only serve as a 'pointer' for future reference (in the code), and it's actually initialized when first used in other functions?

But isn't it being initialized when it goes to compile the private class variables? Or does that only serve as a 'pointer' for future reference (in the code), and it's actually initialized when first used in other functions?

FlyingIsFun1217

There is little wrong with the syntax you use, except that it is not valid C++. Perhaps the designers of the language did not want there to be ambiguity if an object was initialized both as you suggest and in the initializer list of a constructor. Or perhaps they simply did not think of that syntax.

But an object is initialized when you call it's constructor. In this way different objects can be initialized differently, depending on constructor arguments, and each instance has it's own copy of the variables.

But isn't it being initialized when it goes to compile the private class variables? Or does that only serve as a 'pointer' for future reference (in the code), and it's actually initialized when first used in other functions?

FlyingIsFun1217

To be honest, I have very little idea what you think you mean here, but I'll try to answer all the same. When you declare a class, you create a grand total of nothing. You say, "IF someone decides they want a blah, give 'em one of these, and one of those, and a couple of that." If you want each of your objects to start life with isScanning equal to false, then you need to set that when your object is created -- in the constructor. That's what the constructor is for, after all, to set up each of your objects the way you want.

I think that we just have a terminology clash here. The OP has probably seen code like the following before, thinking that the variables are initialised "inside the class" without realising that nothing actually gets initialised until instances are created.

It is done that way in Java. Richie T demonstrates the equivalent C++ syntax.

The problem with the Java syntax is that it does not allow initialization to depend on constructor arguments.

That's the kicker. E.g. for a copy-constructor you would most likely not want an int default initialised to zero (it oyu had written that) before being given the value in the item it is being constructed from.