Reputation: 4820
Like this,
bool isEmpty() const { return root==NULL; }
This is isEmpty function, test if the BST is empty or not.
Upvotes: 2
Views: 2664
Reputation: 206498
It indicates that the function does not modify any of the members of that class.
Usually, the Interface/declaration(through header file) is made available to the users of the class/functions and not the implementation,So the const
makes it clear to the user that the function does not modify any members.
Adding the const
also makes the user of the function aware that this const
member functions should be used when you have an const
object.You cannot call normal member function on a const
object of that class,it will result in a compiler error.
That is the reason that the function is marked const
even if it is empty.It indicates a contract between the function implementer and the user of the function.
Upvotes: 11
Reputation: 182743
It indicates that the function is logically constant, that is, as far as users of the class are concerned, the value of the class member is not changed by the function. It is legal to call const functions on const references and through const pointers.
Upvotes: 0
Reputation: 46183
When a function is marked as const
, the function can be invoked on a const
instance of the class. Invoking a non-const
function on a const
object will lead to a compile-time error.
Basically, you want to mark all functions that don't change the state of your object as const
; this way, you can use const
as an immutability declaration and the compiler will enforce it for you, by making sure you can only invoke the const
functions.
You can invoke const
functions on a non-const
instance no problem.
Upvotes: 2
Reputation: 28563
It tells the compiler that the function will not modify the state of the class. Also, const functions are the only functions allowed to be called on const objects.
Upvotes: 0