类成员的类型定义
typedef for class member
是否可以用类成员/函数做一个typedef?在下面的示例中,我使用 boost bimap 函数来存储有关节点最近邻居的信息。
typedef boost::bimap<float /*distance*/, int /*id*/> neighbor_list;
neighbor_list node_a;
//fill up neighbors of node_a
//get nearest neighbor of node_a
node_a.neighbor.left.begin()->second;
//get distance to the nearest neighbor of node_a
node_a.neighbor.left.begin()->first;
然而,上面的线条看起来很混乱,可能不直观。所以我想知道是否可以为班级成员做一个typedef
,这样我就可以做如下事情
typedef boost::bimap<float /*distance*/, int /*id*/> neighbor_list;
typedef neighbor_list::left::begin()->first nearest_neighbor;
//nearest neighbor of node_a
node_a.nearest_neighbor;
我知道我可以编写自己的函数来封装代码的混乱部分,但我想知道我是否可以为类成员提供别名。
只需将讨厌的分辨率委托给函数即可。
#include <boost/bimap.hpp>
typedef boost::bimap<float /*distance*/, int /*id*/> neighbor_list;
float nearest_neighbor(neighbor_list const& node)
{
return node.neighbor.left.begin()->first;
}
void foo()
{
neighbor_list node_a;
nearest_neighbor(node_a);
}
相关文章:
- 使用QJsEngine在Qt中注册自定义类型
- 在UE4中使用未定义类型'UTextBlock'
- 修改"std::set"中用户定义类型的值
- 当我使用自定义类型创建动态数组时,即使使用字符串,它似乎也不起作用
- QtQuick - qml:28:错误:未知方法返回类型:自定义类型
- 如何使自定义类型在unordered_map中用作键
- 在 C++20 中是否不再允许在 std 中对程序定义类型的函数模板进行专用化?
- Qt5 远程对象 + 自定义类型,但不在 POD 中
- 为什么转换函数声明不需要至少一个定义类型说明符
- 标准::原子中的自定义类型
- 如何使用自定义类型声明Arduino数组?
- 如何在 Cython 中定义返回 cpp 定义类型的函数?
- 使用自定义访问者时具有自定义类型的提升变体失败(源自 boost::static_visitor)
- 您可以将binary_search应用于具有自定义类型的矢量吗?
- 在自定义类型图中重用 SWIG 映射
- 扩展自定义类型的spdlog
- vim使用户定义类型的COLOR与C++中的基本类型相同
- 重载自定义类型的 std::to_string 和 std::to_chars?
- 具有未声明/未定义类型的 typedef 结构
- 函数重载:内置类型与用户定义类型