重载==函数没有被调用
我目前正在努力为我的链接列表重载 == 运算符。我在头文件中设置了如下运算符:
class sqrlst
{
public:
std::vector<int> vlist;
bool operator == (iterator const & rhs )
{
return this->iter == rhs.iter;
};
然后,我使用以下代码在头文件中创建了一个方法
void test()
{
bool flag;
if (vlist.begin()==vlist.begin())
{
flag=true;
}
};
};
但是,当调用此方法时,当它命中 if 语句时,它不会转到我的重载 == 运算符函数。当我将调试点放在重载函数上时,它表示将无法到达该行。
非常感谢任何提示或建议。谢谢!
编辑:vlist 是一个整数列表。
I am currently working on overloading the == operator for my linked list. I have the operator in my header set up like the following:
class sqrlst
{
public:
std::vector<int> vlist;
bool operator == (iterator const & rhs )
{
return this->iter == rhs.iter;
};
I then created a method in my header file with the following code
void test()
{
bool flag;
if (vlist.begin()==vlist.begin())
{
flag=true;
}
};
};
However when this method is called it does not go to my overloaded == operator function when it hits the if statment. When I put the debug point on the overload function it says that the line will not be reached.
Any tips or suggestions are greatly appreciated. Thanks!
EDIT: vlist is a list of ints.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,std::vector 成员函数begin() 和end() 会返回std::vector类型的迭代器。 ::iterator 或
`std::vector::const_iterator
,具体取决于向量对象是const
还是非常量。不管它是什么,迭代器类型都不是由您定义的。在类sqrlist
中重载==
不会执行任何操作。重载==
应该是向量迭代器类的成员,您不允许编辑该类。另请注意,向量的迭代器类已经重载了
==
和!=
运算符。因此,当您使用==
比较迭代器时,它正在调用向量迭代器类的成员函数。Well,
std::vector
member functionsbegin()
andend()
returns iterator of typestd::vector<T>::iterator
, or`std::vector<T>::const_iterator
, depending on whether the vector object isconst
or non-const. Whatever it is, the iterator type is not defined by you. Overloading==
in your classsqrlist
does nothing. The overload==
should be a member of vector's iterator class, which you're not allowed to edit.Also note that vector's iterator class has already overloaded
==
and!=
operators. So when you compare iterators using==
, it is invoking a member function of vector's iterator class.