Initialization vs. Assignment

This is another of the places where it's important to differentiate between initialization and assignment. We can't assign a value to a const, but we can initialize it; in fact, because an uninitialized const is useless, the attempt to define a const without specifying its initial (and only) value is a compile-time error. In this case, we're initializing it to the value 256; if we just wrote const short BUFLEN;, we'd get an error report something like the one in Figure 8.35 when we tried to compile it.

Figure 8.35. Error from an uninitialized const (code\string5x.err)
STRING5X.cpp:
Error E2304 STRING5X.cpp 82: Constant variable 'BUFLEN' must be initialized in function
 operator >>(_STL::istream &,string &) Error ...

Get C++: A Dialog Programming with the C++ Standard Library now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.