QMap的这种用法有潜在的危害吗?
Is this use of QMap potentially harmful?
#include <QMultiMap>
template <typename TKey, typename TValue>
TKey lastKeyOf(const QMap<TKey, TValue>& map)
{
if (map.isEmpty())
throw something;
return (map.end() - 1).key();
}
我问的原因是:
template <typename TKey, typename TValue>
QMultiMap<TKey, TValue>;
公开继承QMap<TKey, TValue>
。如果我调用:
QMultiMap<int, std::string> multimap;
...
lastKeyOf(multimap);
lastKeyOf
内部的所有调用都静态地绑定到它们的QMap
版本而不是QMultiMap
版本,因为QMap
不是用于多态使用(没有虚拟析构函数)。
我甚至不知道这个用法叫什么。是对象切片吗?
我相信这应该是安全的。QMultiMap只是在QMap中添加了一组新的重载方法,只有当你在lastKeyOf()中使用一个在QMultiMap中重载的具有不同行为的函数时,你才会得到意想不到的结果。我能看到的唯一具有兼容签名和不同行为的重载方法是insert()。
话虽如此,避免使用QMultiMap和QMap::insertMulti()而不是QMap::insert()来使用QMap可能会使代码更简单。
相关文章:
- 这个指针在c++中的用法
- 当C++中需要自动删除时,这是静态的正确用法吗?
- libstdc++ 文件系统中未初始化的用法?
- 复制和交换习惯用法与移动操作之间的交互
- 类作用域的类型别名"using":[何时]方法中的用法可以先于类型别名?
- 命名参数习惯用法和(抽象)基类
- 省略号在C++中的所有用法
- () 在 C++ 11 中的特殊而奇怪的用法?
- 构造函数的用法
- 模板类中自动的类用法不完整
- 这里的 = 运算符有什么用法?
- 将自定义 QMap 模板实例化以人类可读的形式保存在 QSettings 中
- 什么模板用法在阶乘中更好
- 如果可变长度元素的constexpr用法得到<>
- 为什么 setjmp/longjmp 的这种用法是未定义的行为?
- 访问提升:shared_ptr 主范围外崩溃,断言失败:px != 0.指针的正确用法是什么?
- C++ 中函数中 Const 用法之间的差异
- equal_to模板类C++用法
- PCL 中的 GICP 用法
- QMap的这种用法有潜在的危害吗?