c – 重载*作为解除引用
发布时间:2020-12-16 09:51:36 所属栏目:百科 来源:网络整理
导读:我很难尝试重载*运算符.我试图使用它来取消引用指针.我已经发布了我正在尝试使用的内容.现在,当我尝试使用它时,我得到以下错误间接需要指针操作数(‘Iterator’无效) //用法 Iterator List::Search(int key) { Iterator temp(head); while (!temp.isNull())
我很难尝试重载*运算符.我试图使用它来取消引用指针.我已经发布了我正在尝试使用的内容.现在,当我尝试使用它时,我得到以下错误间接需要指针操作数(‘Iterator’无效)
//用法 Iterator List::Search(int key) { Iterator temp(head); while (!temp.isNull()) { if (*temp == key) { //return temp; cout << *temp << endl; } temp++; } return NULL; } //头文件 class Iterator { public: Iterator &operator *(const Iterator &) const; private: node* pntr; }; // CPP文件 Iterator &Iterator::operator *(const Iterator & temp) const { return temp.pntr; } 解决方法
一元反复数运算符不需要参数.它也不太可能返回Iterator的引用.在这种情况下,我希望它返回对节点的引用.请注意,允许通过const运算符对数据进行可变访问,并提供仅允许const访问的ConstIterator类型是惯用的:
class Iterator { public: node& operator*() const; node* operator->() const; private: node* pntr; }; node& Iterator::operator*() const { return *pntr; } node* Iterator::operator->() const { return pntr; } node& Iterator::operator*() { return *pntr; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |