的错过载我有这样的代码:C++编译器选择一个类的成员函数
template <class T>
class Something
{
T val;
public:
inline Something() : val() {}
inline Something(T v) : val(v) {}
inline T& get() const { return val; }
inline Something& operator =(const Something& a) { val = a.val; return *this; }
};
typedef Something<int> IntSomething;
typedef Something<const int> ConstIntSomething;
class Other
{
public:
IntSomething some_function()
{
return IntSomething(42);
}
ConstIntSomething some_function() const
{
return ConstIntSomething(42);
}
};
void wtf_func()
{
Other o;
ConstIntSomething s;
s = o.some_function();
}
但是,编译器拾取的在wtf_func()
Other::some_function()
错误的过载(即,非const之一)。我怎样才能解决这个问题?请注意,由于某些原因,我无法更改Other::some_function()
的名称。
这是一个有点误导,因为你可以有'常量非'const'对象'会员功能,你可以给他们打电话。 –
@Seth Carnegie:没错,只是为了说清楚。你可以在非const对象上调用const成员函数,但是你不能在const对象上调用非const成员函数。 –