In lesson 4.13 -- Const variables and symbolic constants, you learned that fundamental data types (int, double, char, etc…) can be made const via the const keyword, and that all const variables must be initialized at time of creation. Show In the case of const fundamental data types, initialization can be done through copy, direct, or uniform initialization:
Const classes Similarly, instantiated class objects can also be made const by using the const keyword. Initialization is done via class constructors:
Once a const class object has been initialized via constructor, any attempt to modify the member variables of the object is disallowed, as it would violate the const-ness of the object. This includes both changing member variables directly (if they are public), or calling member functions that set the value of member variables. Consider the following class:
Both of the above lines involving variable something are illegal because they violate the constness of something by either attempting to change a member variable directly, or by calling a member function that attempts to change a member variable. Just like with normal variables, you’ll generally want to make your class objects const when you need to ensure they aren’t modified after creation. Const member functions Now, consider the following line of code:
Perhaps surprisingly, this will also cause a compile error, even though getValue() doesn’t do anything to change a member variable! It turns out that const class objects can only explicitly call const member functions, and getValue() has not been marked as a const member function. A const member function is a member function that guarantees it will not modify the object or call any non-const member functions (as they may modify the object). To make getValue() a const member function, we simply append the const keyword to the function prototype, after the parameter list, but before the function body:
Now getValue() has been made a const member function, which means we can call it on any const objects. For member functions defined outside of the class definition, the const keyword must be used on both the function prototype in the class definition and on the function definition:
Futhermore, any const member function that attempts to change a member variable or call a non-const member function will cause a compiler error to occur. For example:
In this example, resetValue() has been marked as a const member function, but it attempts to change m_value. This will cause a compiler error. Note that constructors cannot be marked as const. This is because constructors need to be able to initialize their member variables, and a const constructor would not be able to do so. Consequently, the language disallows const constructors. Const member functions can also be called by non-const objects. Best practice Make any member function that does not modify the state of the class object const, so that it can be called by const objects. Const objects via pass by const reference Although instantiating const class objects is one way to create const objects, a more common way to get a const object is by passing an object to a function by const reference. In the lesson 9.5 -- Pass by lvalue reference, we covered the merits of passing class arguments by const reference instead of by value. To recap, passing a class argument by value causes a copy of the class to be made (which is slow) -- most of the time, we don’t need a copy, a reference to the original argument works just fine, and is more performant because it avoids the needless copy. We typically make the reference const in order to ensure the function does not inadvertently change the argument, and to allow the function to work with R-values (e.g. literals), which can be passed as const references, but not non-const references. Can you figure out what’s wrong with the following code?
The answer is that inside of the printDate function, date is treated as a const object. And with that const date, we’re calling functions getYear(), getMonth(), and getDay(), which are all non-const. Since we can’t call non-const member functions on const objects, this will cause a compile error. The fix is simple: make getYear(), getMonth(), and getDay() const:
Now in function printDate(), const date will be able to successfully call getYear(), getMonth(), and getDay(). Const members can not return non-const references to members When a member function is const, the hidden *this pointer is also const, which means all members are treated as const within that function. Therefore, a const member function can not return a non-const reference to a member, as that would allow the caller to have non-const access to that const member. Const member functions can return const references to members. We’ll see an example of this in the next section. Overloading const and non-const function Finally, although it is not done very often, it is possible to overload a function in such a way to have a const and non-const version of the same function. This works because the const qualifier is considered part of the function’s signature, so two functions which differ only in their const-ness are considered distinct.
The const version of the function will be called on any const objects, and the non-const version will be called on any non-const objects:
Overloading a function with a const and non-const version is typically done when the return value needs to differ in constness. In the example above, the non-const version of getValue() will only work with non-const objects, but is more flexible in that we can use it to both read and write m_value (which we do by assigning the string “Hi”). The const version of getValue() will work with either const or non-const objects, but returns a const reference, to ensure we can’t modify the const object’s data. Summary Because passing objects by const reference is common, your classes should be const-friendly. That means making any member function that does not modify the state of the class object const! How do you initialize a member variable?To initialize a class member variable, put the initialization code in a static initialization block, as the following section shows. To initialize an instance member variable, put the initialization code in a constructor.
What is used to declare and initialize an object from another object?A copy constructor is a member function that initializes an object using another object of the same class.
Which of the following is used to initialize the attributes of object using default values?The no-argument constructor usually sets the attributes of the object to default values.
What is member initializer list in C++?Member initializer list is the place where non-default initialization of these objects can be specified. For bases and non-static data members that cannot be default-initialized, such as members of reference and const-qualified types, member initializers must be specified.
|