These are some basic and quite interesting things about const where a programmer can does mistakes. Although I have tried to make all the concepts correct , but if anybody find any incorrect information in it , then please let me know.
1.
most fundamental things about const :
a) const fred* const ptr : neither ptr nor *ptr can changed
b) fred * const ptr : ptr can't be changed but *ptr can't be changed
c) const fred * ptr : ptr can change but *ptr can't be changed
d)const fred & kk : const fred& is a reference to a fred that can't be changed via that reference ( here kk )
for ex:
calss fred { public: int i; fred(){i=0;} }
int main()
{
fred kk1,kk2;
const fred & kk=kk1;
kk1.i=5;
//kk.i=10;
return 0;
}
here kk.i will prompt for an error.
fred & const : doesn't make sense, because we can never rebind the reference so that it refers to a different object.
2.
Const Correctness :
If we have to avoid making unexpected changes to objects , then it can be done by proper using of keyword const. The c++ compiler detects many unexpected changes to objects and it flags these violations with error messages at compile time.
for ex:
void f( const string& s)
throw(); // parameter is received by reference to const
3.
Const correctness : It should be done sooner rather than later ( to avoid the ripple effect in function calling, where one function contain oth rs )
4.
Inspector & Mutator class:
fred {
public:
int kk() ; // mutator
int kk1() const ; // inspector
}
An inspector is a member function that returns information about an object's state without changing the object's abstract state. A mutator changes the object's abstract state.Whenever the member function wants to guarntee that it won't make any observable changes to its "this" object , then we make this member function constant.
5.
Const, formal parameter and pass by value :
We should not use const for formal parameter types, that are passed by value, because a const on a pass by value parameters affects only the code inside the function, it doesn't affect the caller. For ex,
replace f(const fred x) with either f(const fred &x ) or f(fred x)
replace f(fred * const p) with f(fred * p).
6.
If we want to change the data member inside const member function, the we have to make that data member mutable or if it can't be done then we should use const_cast.
1.
most fundamental things about const :
a) const fred* const ptr : neither ptr nor *ptr can changed
b) fred * const ptr : ptr can't be changed but *ptr can't be changed
c) const fred * ptr : ptr can change but *ptr can't be changed
d)const fred & kk : const fred& is a reference to a fred that can't be changed via that reference ( here kk )
for ex:
calss fred { public: int i; fred(){i=0;} }
int main()
{
fred kk1,kk2;
const fred & kk=kk1;
kk1.i=5;
//kk.i=10;
return 0;
}
here kk.i will prompt for an error.
fred & const : doesn't make sense, because we can never rebind the reference so that it refers to a different object.
2.
Const Correctness :
If we have to avoid making unexpected changes to objects , then it can be done by proper using of keyword const. The c++ compiler detects many unexpected changes to objects and it flags these violations with error messages at compile time.
for ex:
void f( const string& s)
throw(); // parameter is received by reference to const
3.
Const correctness : It should be done sooner rather than later ( to avoid the ripple effect in function calling, where one function contain oth rs )
4.
Inspector & Mutator class:
fred {
public:
int kk() ; // mutator
int kk1() const ; // inspector
}
An inspector is a member function that returns information about an object's state without changing the object's abstract state. A mutator changes the object's abstract state.Whenever the member function wants to guarntee that it won't make any observable changes to its "this" object , then we make this member function constant.
5.
Const, formal parameter and pass by value :
We should not use const for formal parameter types, that are passed by value, because a const on a pass by value parameters affects only the code inside the function, it doesn't affect the caller. For ex,
replace f(const fred x) with either f(const fred &x ) or f(fred x)
replace f(fred * const p) with f(fred * p).
6.
If we want to change the data member inside const member function, the we have to make that data member mutable or if it can't be done then we should use const_cast.
