M
matthias_k
Hi,
I've never thought about this before, but since you are able to overload
a function only by changing the const-ness of the formal parameters,
some annoying side effects will arise.
For example, if you only have this code ...
void foo( const int n ) {}
int a;
foo( a );
.... the compiler will not complain about the non-constness of a.
This is not surprising, I always treated the const qualifier for a
parameter as some kind of contract between me an d the client that the
actual parameter will never be modified.
However, if you overload foo, like this ...
void foo( int n ) {} // 1
void foo( const int n ) {} // 2
int a;
foo( a ); // 1
int const b;
foor( b ); // 2
.... the compiler suddenly cares about the const-ness of the argument.
So, if you initially have this code:
void foo( const int n ) {}
int a;
foo( a );
and suddenly some coder comes along and overloads foo in the way
mentioned above, a completely different function is called!
Maybe it's not such a good idea afterall to make parameters const in
order to imply non-modifying behavior??
Eager to hear your opinions,
- Matthias
I've never thought about this before, but since you are able to overload
a function only by changing the const-ness of the formal parameters,
some annoying side effects will arise.
For example, if you only have this code ...
void foo( const int n ) {}
int a;
foo( a );
.... the compiler will not complain about the non-constness of a.
This is not surprising, I always treated the const qualifier for a
parameter as some kind of contract between me an d the client that the
actual parameter will never be modified.
However, if you overload foo, like this ...
void foo( int n ) {} // 1
void foo( const int n ) {} // 2
int a;
foo( a ); // 1
int const b;
foor( b ); // 2
.... the compiler suddenly cares about the const-ness of the argument.
So, if you initially have this code:
void foo( const int n ) {}
int a;
foo( a );
and suddenly some coder comes along and overloads foo in the way
mentioned above, a completely different function is called!
Maybe it's not such a good idea afterall to make parameters const in
order to imply non-modifying behavior??
Eager to hear your opinions,
- Matthias