如何区分对象和const对象
How to distinguish between object and const object
我试图在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) { ... }
相关文章:
- 被解释为低级别const的const对象的地址
- 使用共享指针的函数调用,其对象应为 const
- 如何从构造函数副本 T(const T&)调用对象 T?
- 在 C++ 中声明 const 对象需要用户定义的默认构造函数.如果我有一个可变成员变量,为什么不呢?
- Google Or-Tools Glop:如何创建指向 const 对象的指针数组?
- 无法使用类型 'const char *' 的左值初始化类型 'char *' 的成员子对象
- 我们可以有一个 setter 成员函数作为从 const 对象引用的 const 吗?
- "this"指向的对象是否与 const 对象相同?
- const auto & 和 auto & if reference 对象之间的区别是 const
- 将对象传递给函数而不将其包装到 std::ref 中,而参数被指定为 const 引用
- C++ - 确定 const char* 是指向字符串文本对象还是动态对象
- 为什么 const YAML::Node 对象的行为不像 yaml-cpp 中的类似值的对象?
- 从 const 对象访问非 const 方法
- 将非 const 对象用于 const 参数
- 如何键入用于const对象的自定义io操纵器
- 对临时对象的Const引用不会延长其生存期
- 正在通过const-ref未定义的行为捕获新构造的对象
- 具有成员变量的对象 Const 数组 = 先前索引成员变量的总和
- 为什么我构建的临时对象const不可变
- 强制用户声明对象const