我有以下代码:
#include <iostream>
struct Base {
int i_;
};
class El : protected Base {
public:
int get_i() const { return i_; }
void set_i(int i) { i_ = i; }
};
class It : protected Base {
public:
using pointer = const El*;
using reference = const El&;
reference operator*() const
{
return reinterpret_cast<reference>(*this);
}
pointer operator->() const
{
return reinterpret_cast<pointer>(this);
}
};
int main()
{
It it;
It* itp = ⁢
std::cout << *****(itp)->get_i() << "n"; //ERROR
}
GCC和Clang++都无法调用operator*
或operator->
,所以无论我尝试了多少间接寻址,我在最后一行都会得到一个错误It doesn't have member function 'get_i'
。标准是否保证了这种非直觉的行为?
运算符优先级:->
绑定更紧密,因此应用于指针itp
。
重载operator->
时,不会影响应用于类指针的operator->
的含义。我想你想要(*itp)->get_i();
。