使用引用和指针重载指针 return
Overload return of a pointer with reference and pointer
我得到以下代码:
class myarray
{
float* rawdata;
float* GetRawData () const noexcept { return rawdata; };
float& GetRawData () const noexcept { return rawdata; };
const float& GetRawData () const noexcept { return rawdata; };
}
我收到编译器错误,提示无法重载前两个成员函数。 C++ 是否无法区分,因为我们在引用中调用函数时会有不同的语法
Is it not possible for C++ to distinguish
不是。可以使用不同的参数列表进行重载,对于成员函数,ref/const 限定符。但不是 return 值。您的重载具有相同的参数列表和 ref/const 限定符,因此它们不能相互重载。
since we in a reference would have a different syntax when invoking the function
不,我们不会。两种情况下的调用是相同的:
this->GetRawData()
我得到以下代码:
class myarray
{
float* rawdata;
float* GetRawData () const noexcept { return rawdata; };
float& GetRawData () const noexcept { return rawdata; };
const float& GetRawData () const noexcept { return rawdata; };
}
我收到编译器错误,提示无法重载前两个成员函数。 C++ 是否无法区分,因为我们在引用中调用函数时会有不同的语法
Is it not possible for C++ to distinguish
不是。可以使用不同的参数列表进行重载,对于成员函数,ref/const 限定符。但不是 return 值。您的重载具有相同的参数列表和 ref/const 限定符,因此它们不能相互重载。
since we in a reference would have a different syntax when invoking the function
不,我们不会。两种情况下的调用是相同的:
this->GetRawData()