Hi!
I know the meaning of the const in the parameter field but I don't know exactly what it means if you declared it after the parameter field.
Code:bool find(const String &str) const; // what does this const do? bool find(const String &str);
Hi!
I know the meaning of the const in the parameter field but I don't know exactly what it means if you declared it after the parameter field.
Code:bool find(const String &str) const; // what does this const do? bool find(const String &str);
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
const after a function name is only relevant for a method of a class. With regular functions, you can make all the parameters const to prevent the function from changing them. However, with a method of a class, there is another entity involved--the object that called the method. What if you want to ensure that the method does not change the object? To do that, you put const after the method to enforce your desire that the method not change the object.
Only constant methods can be called by objects that were declared as constant. A constant object has to be absolutely sure a rogue method won't harm it before entering its lair. On the other hand, objects that aren't declared as constant can happily call a constant method--they don't care whether they are changed or not, so they could care less that a function guarantees they won't be changed.
Last edited by 7stud; 05-10-2005 at 09:19 AM.