The information in this article applies to:
The sample source code below demonstrates how to initialize a const member of a class. Note that the code uses a member initialization list because a const member may not appear on the left side of the assignment operator. The value must be set when the object is constructed because the value of a const member can be set only at initialization. If an application attempts to initialize a const member with the assignment operator, the compiler generates the following error messages: Using Visual C++ 32-bit Edition, version 4.0, this error is reported as: The member initialization list is the preferred method to initialize the members of a class. For example, when initializing an object of a user- defined type, it is more efficient to use a member initialization list to initialize a data member than to perform an assignment in the constructor. The latter approach requires two calls: one to construct the data member and another to the operator=() function to assign a value to the member. Sample Code
Output
Additional query words:
Keywords : kbLangCPP kbVC100 kbVC150 kbVC151 kbVC200 kbVC210 kbVC400 kbVC500 |
Last Reviewed: July 6, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |