Constant Objects Constant Member Functions


Constant Objects & Constant Member Functions

Constant Objects

In C++, it is possible to not only have constant system data instances but constant class objects as well. Constant objects may be defined using the following construct:

image from book

 const ClassName classObject; 

image from book

In order for a class object to be a constant, the class must have an explicitly constructor that will initialize the object when it is defined.

Constant objects are rvalues and cannot therefore be used on the left side of an assignment operator. By defining an object to be a constant, we imply that we do not want to modify the data members of the object except in a very controlled way. When we studied constant variables previously, the variables could only be an lvalue when they are defined. The same is true for constant objects.

Constant Member Functions

By using the const modifier on an object definition, the programmer is declaring that not all member functions can be dotted with the constant object. The programmer determines which functions can access the constant objects. These functions are denoted as constant member functions using the keyword const. Any method other than a constant member functions that attempts to access a constant object causes a compiler error or warning. By default the constructors and the destructor are constant member functions. That is these functions have the power to not only access but to modify constant objects. A class member function becomes a constant function (and thereby they are the only ones able to access a constant class object) by placing the keyword const between the header and the body of the function in the definition and between the header and the semicolon in the prototype, if a prototype is used.

For example, the construct of the definition of a constant function is:

image from book

 outputDataType ClassName::functionName(argumentList) const {   ..... // function body } 

image from book

and the following is an example of a prototype for a constant method:

image from book

 outputDataType ClassName::functionName(argumentList) const; 

image from book

If a function has both a prototype and a definition, both must have the word const. See example const.cpp.

A class member function that is a constant function may also access non constant objects.

A class member function that is defined as a constant can have the same name as any other member function and therefore may overload the name. The compiler will decide which one to use by determining when the calling object is a constant and when it is not. When a constant function is overloaded with a non constant function, they can have the same signature. The function name mangling is determined by the existence of the keyword const.




Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
EAN: N/A
Year: 2007
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net