litb's solution is right thereon.
However, I would like to do my bit to elaborate on what the compiler message actually means and how to decipher it.
So, here is a longer explanation of the error message and how to understand it in this context. I hope all the gurus here would correct my understanding.
"error C2662: 'Cfoo::GetNum' : cannot
convert 'this' pointer from 'const
Cfoo' to 'Cfoo &' Conversion loses
qualifiers"
- The type of
'bar' is 'Cfoo const *'.
A member function such as 'GetNum' in OP is considered to be declared as
int Cfoo::GetNum(Cfoo &dummyimpliedobjectparameter); // the implied object argument as per 13.3.1/3 and /4
In accordance with 13.3.1.1.1/2
The function call bar->GetNum() is treated as (*bar).GetNum(*bar) where (*bar) is the implied object argument
Now, this means that, an object of type 'Cfoo const' has to be bound to a reference of type 'Cfoo &', to match the function call argument to the function parameter. As per 8.5.3/5', this is not allowed as a reference to non const cannot bind to a const.
So as litb suggested, the way to bail out is to make Cfoo::GetNum as a const member function. As per 13.3.1/3, with this change, the member function Cfoo::GetNum now is considered as
int Cfoo::GetNum(Cfoo const &dummyimpliedobjectparameter); // note the const
Now, the member function 'call' and 'parameter' match exactly and the code is well-formed.
@Steven: Does the compile error make more sense
now?