Ben Voigt [C++ MVP] wrote:
"Peter Duniho" <Np*********@Nn OwSlPiAnMk.comw rote in message
news:13******** *****@corp.supe rnews.com...
>cody wrote:
>>It is possible to declare and use/instantiate a class with a
uninitializ ed readonly field without even a compiler warning. Why don't I
get warnings?
It's not uninitialized, that's why.
[...]
>>I would even say that also for normal class fields there should be a test
that if nowhere in the class an assignment to that variable is found, a
warning should be produced. [...]
Same thing as above. The variable is initialized, so emitting a warning
saying that it's not initialized would be incorrect.
Which language?
With C#, you'd typically get a warning alow the lines of "variable xyz is
never written and will always have its default value null".
That's not a warning saying that the variable is uninitialized. It's
saying that it _is_ initialized, to the default value.
It's true, I should have read the original post more carefully. The
author seems to be saying that NO warning is provided, while I
interpreted it as saying that there's no warning about the uninitialized
variable.
There definitely is a warning, assuming the read-only field is not
explicitly assigned anywhere. It's just not saying that the variable is
uninitialized.
IMHO, the OP confuses the issue by including the second scenario, which
has even more reason for not generating any warning. But certainly even
in that case too, a warning about an uninitialized variable is unwarranted.
Or is it assigned in some constructors and not others?
Having a constructor that initializes a readonly field will suppress the
"default value" warning, yes. After all, the warning is simply saying
that there's _no_ way for the field to be anything other than the
default value. The lack of a warning doesn't mean that the field will
_always_ be something other than the default value.
But the warning is talking about the value that _is_ used to initialize
the field, not whether the field is initialized at all.
Perhaps this is the case the OP is running into in which _no_ warnings
are produced. But even if a warning is produced, it won't be one that
mentions an uninitialized field.
Pete