Question
What happens if you forget the const in an accessor function (an accessor queries the object for information without changing any of the data members)?
What happens if you forget the const in an accessor function (an accessor queries the object for information without changing any of the data members)? What happens if you accidentally supply a const in a mutator function (a mutator is a function that modifies the data members of the object)? Answer (no const in an accessor): Answer (const in a mutator):
Sol56:
If you forget the const
in an accessor function, the compiler may give you an error if you try to call the function on a const
object. This is because the compiler considers the function as modifying the object, which is not allowed for const
objects. Adding const
to the function signature is a way to tell the compiler that the function is only accessing (and not modifying) the data members.
If you accidentally supply a const
in a mutator function, the compiler will give you an error because the function is not allowed to modify the object\'s data members when it is marked as const
. The const
keyword tells the compiler that the function should not change the object\'s state, so it won\'t allow any modifications to be made. This error can be fixed by removing the const
keyword from the function signature.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started