I thought that C++ allows non-const to const conversion in function parameters, such as:
You are trying to do the exact opposite: Const to non-const. Calling a non-const member function, the compiler will bind the expression (which is a Node const
to a Node&
for binding the this
pointer). It thus will drop const - not allowed because it would call a non-const function on a const object.
/* class Foo */ {
Foo(int bar);
}
/* Foo:: */ Foo(const int bar)
{
//lala
}
Well that code is a whole other thing. It declares a function (constructor) two times, with the only difference being that one time the parameter is const, and another time it isn't. The type of the function is the same both times, and so they don't conflict (the const
parameter will only have an impact within the function body locally - it won't make any difference to the caller). In addition, this code doesn't contain any call (assuming that the first block is within some function).
If you wonder: If the above two versions are identical - why isn't the below? Then this is because the below contains one other level of indirection: Below, the reference itself (the top level) is not const (you can't put const
on a reference itself directly), but the type referenced is const. At this time, const is not ignored in determining the type of the function.
/* class Foo */ {
Foo(int &bar);
}
// different thing, won't work!
/* Foo:: */ Foo(const int &bar)
{
//lala
}