<NOSCRIPT><A href="http://about.com/" target=_blank><IMG src="C++ Tutorial - Lesson 22 Mutable Members_files/0.gif" border=0></A></NOSCRIPT>   About > Computing & Technology > C/C++ 
Search     
 Hi, I'm John Kopp, your guide to C, C++ and C# programming. This site provides what you need to know to learn C, C++ or C# programming. Browse the subjects on the left, try one of the tutorials or use the search box to get started.

C/C++

with John Kopp
Your Guide to one of hundreds of sites
 Home · Articles · Forums · Chat · Newsletters · Help    
Subjects

  ESSENTIALS
· C Tutorial
· C++ Tutorial
· C/C++ Glossary
· Tips
· More Tutorials
  BUYER'S GUIDE

Product Reviews
Top Picks
C Tutorials
C++ Tutorials
C#
Advanced C
Advanced C++
Beginning C
Beginning C++
Books
C++ Builder
Careers
CGI
CM
Dictionaries
CompilingDebugging
Freeware/Shareware
Humor
Magazines
OOAD
Polls
SoftwareEngineer'n
Style
STL
UNIX/GNU
Visual C++

Subject Library

All articles on this topic

 

Stay up-to-date!
Subscribe to our newsletter.

Web Hosting
Global Servers

 
 
 
Advertisement
> Free Credit Report
 
C++ Tutorial - Lesson 22: Mutable Members
 More of this Feature
• Introduction, Const Keyword
• Example Program
• Mutable Keyword
 
 Related Resources
• C++ Tutorial - Constants
• C++ Tutorial - Introduction to Classes
• Advanced C++ Tutorials
• Simple Solutions in C++
• C++ Programming Tips
• C++ Tutorial - See all lessons
 

by John Kopp

Introduction
Welcome to About.com's free tutorial on C++ programming. This lesson deals with two important keywords, const and mutable, and their use with class objects. Const provides a way to declare that an object is not modifiable. It can also be used with class methods to indicate to they will not modify their objects. The use of "const" can reduce bugs within your code by allowing the compiler to catch unintended modification of objects that should remain constant. The keyword mutable provides a way to allow modifications of a particular data members of a constant objects. By the way, I love tabloid style headlines, which is why this lesson is titled "Mutable Members" rather than something dull like "Const and Mutable"

Const
In an earlier lesson, the keyword const was used to declare that an object of built-in type, that is, an ordinary variable, was a constant. Attempts to modify a "const" result in a compilation error.

const int limit;
limit = 25;    // Results in compilation error

Assigning to a const variable is not permitted. It must be initialized to provide a value.

const int limit = 25;

This provided a way to have a constant and to be sure that the constant was not modified unintentionally. C++ also allows the declaration of const class objects.

// Class Definition
class Employee {
public:
    string getName(){return _name;}
    void setName(string name) {_name = name;}
    string getid(){return _id;}
    void setid(string id) {_id = id;}
private:
    string _name;
    string _id;
};

const Employee john;

This declares the object john of class Employee to be constant. But there are problems with this code. First, since the object is const, we need a way to initialize it. Its members cannot be assigned either directly or indirectly via methods. The compiler's default constructor is insufficient, so we must define constructors that can initialize all the data members. A default constructor that initializes all members is required. Other constructors may be written. Second, C++ allows methods to be declared as const. By declaring a method "const" we are in effect stating that the method cannot change its object. Only methods declared const can be called be a const object. This has real benefit. The compiler can check that methods declared const do not modify the object. Attempts to modify an object within a const method will be flagged by the compiler. Since a const object may invoke only const methods, it will not be modified unintentionally. Objects that are not const can invoke both const and non-const methods. Which methods should be declared as const? Certainly, any method that is intended to simple return the value of a data member should be. Depending upon their purpose, the const keyword may be appropriate for other methods as well. Let's correct the Employee class to allow its proper use with const Employee objects and see its use in a simple program.



Next page > Const, Continued > Page 1, 2, 3


Subscribe to the C/C++ Newsletter
Name
Email






Email this page | Sign up for a Newsletter |
Explore More
Most Popular Articles
• C++ Programming Tutorial
• C Programming Tutorial
• C++ Tutorial - Lesson 12: File Input and Output
• C++ Tutorial - Lesson 4: Input and Output - cin, cout, iostr...
• C++ Tutorial - Lesson 10: Arrays and Vectors
• C++ Tutorial - Lesson 1: Writing and Compiling A First Progr...
• Tutorials
What's Hot Now
• Teach Yourself C++ in 21 Days by Jesse Liberty
• C Programming Tutorial
• C++ Tutorial - Lesson 25: Operator Overloading
• The C Programming Language, 2nd Edition by Kernighan and Rit...
• Topics in C++: Function Template Specialization
• C Programming Tips: Using Long Constants
• C Tutorial - Lesson 5: Conditional Processing, Part 1 - Rela...
About Us | Advertise on This Site | User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2004 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.