如何区分对象和const对象

How to distinguish between object and const object

本文关键字:对象 const 何区      更新时间:2023-10-16

我试图在c++中实现模板链表,假设有迭代器和构造器(不能修改列表)。我需要调用的方法返回一个匹配列表对象的迭代器。例如,如果用const列表调用begin(),它将返回constator,而用非const列表调用begin(),它将返回Iterator。

我该怎么做?

谢谢。

像这样:

ConstIterator begin() const;
Iterator begin(); //  ^^^^^

还可以加上cbegin:

ConstIterator cbegin() const;

重载函数:

 Iterator getIterator();
 ConstIterator getIterator() const;

普通c++重载将完成此操作。例如:

ConstIterator GetList(const List* x) const { ... }
Iterator GetList(List* x) { ... }