Pages

Friday, December 05, 2008

About 'const' in object-oriented paradigm

Last week, my colleague ask me, 'what is the purpose of const in enforcing least privilege in C++?' (referring to formal parameter of function)

At first, i'm quite surprised to heard this term 'least privilege'. But then after googling i found on WIKI that this refers to 'limiting access to resources' or known as 'principle of least privilege'.

So IMHO, in C++, the used of const on formal parameter is to :
1) avoid the function from modifying its parameter's (argument's) value
2) as good indication to simplify programming in object-oriented, since we don't have to go through the whole function to verify that the function don't modify the argument

There are more uses of const in C++. Check your documentation for details. But i lists some of them here:

1) declare constant variable.
2) const on (global) function declaration is to allow the function to access constant variable.
3) const on class method's declaration is to indicate that the function (at least) used class' property(s), but never modifies any of it.

-EOF

No comments:

Post a Comment