If I do this:
// In header class Foo { void foo(bar*); }; // In cpp void Foo::foo(bar* const pBar) { //Stuff }
The compiler does not complain that the signatures for Foo::foo do not match. However if I had:
void foo(const bar*); //In header void Foo::foo(bar*) {} //In cpp
The code will fail to compile.
What is going on? I’m using gcc 4.1.x
The const keyword in the first example is meaningless. You are saying that you don’t plan on changing the pointer. However, the pointer was passed by value and so it dos not matter if you change it or not; it will not effect the caller. Similarly, you could also do this:
You can even do this:
Since the int is passed by value, the constness does not matter.
In the second example you are saying that your function takes a pointer to one type, but then implement it as taking a pointer to another type, therefore it fails.