CONSTantly looking.

Started by
3 comments, last by CaffieneFreak 20 years, 8 months ago
I've been looking around in every book I have, on other websites, everywhere, for information on the following. What is the difference between the following definitions: void aClass::aFunction(); and void aClass::aFunction() const; Any ideas? To be, or not to be, those are the parameters. ----------------------------------------------------- To be, or not to be, those are the parameters. [edited by - CaffieneFreak on August 16, 2003 11:50:51 PM]
Advertisement
''const'' is used like that to declare member functions which don''t change the state of any of the class'' member variables. For example, accessor functions are almost always const(or should be.) Its good to use when you don''t change member variables; It describes your interface better and maybe leads to quicker compiled code.

Ravyne, NYN Interactive Entertainment
[My Site][My School][My Group]

throw table_exception("(? ???)? ? ???");

Cool, thank you. Its fairly obvious now I know the answer



-----------------------------------------------------

To be, or not to be, those are the parameters.
Also, if you have a const object and invoke that method, the const flavor will be invoked.

e.g.

T& operator[](int i) {return vec[i];const T& operator[](int i) const {return vec[i];}


That way with a const vector, you can still index the array, but can''t change it.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
quote:Original post by Magmai Kai Holmlor
Also, if you have a const object and invoke that method, the const flavor will be invoked.

e.g.

T& operator[](int i) {return vec[i];const T& operator[](int i) const {return vec[i];}


That way with a const vector, you can still index the array, but can''t change it.

Neat trick. Thanks.

This topic is closed to new replies.

Advertisement