c++ - 从指针向量的引用访问元素
问题描述
我有一个指针向量的引用,如下所示:
std::vector<Object*>& objects;
如何从此向量访问对象?
解决方案
That objects
is a reference has no direct bearing on the question -- you use a reference in exactly the same way you use a simple identifier for the same object.
Thus, you can select an individual element of the vector by any of the normal means, such as by index, by iterator, or whatever. The result is of the vector's element type, which is Object *
:
Object *o = objects[42];
You can access the object to which o
points via the dereference operator (unary *
). Alternatively, you can use the indirect access operator (->
) to access its methods and fields:
(*o).do_something();
int i = o->an_int;
推荐阅读
- python - 无法 SetField ogr OFTDate 字段,如何设置 OFTDate 字段
- javascript - Jquery 选择器 - 我怎样才能确保这有效?
- c - 如果所有相关线程在使用资源之前都尝试获取它们应该获取的锁,互斥锁是否才能正常工作?
- angular - 会话在后端工作正常,但是当我使用前端发送请求时它不起作用
- c++ - 在 c++ 中解释多维向量语句时感到困惑
- android - 为什么我在 Android 中的 RecyclerView 没有正确显示视图?
- templates - 在 Hugo 中有什么方法可以将页面标题放在范围声明中
- octobercms - 如何在 october cms 中定义可重用函数?
- google-cloud-platform - 在 Terraform 日志记录接收器中拒绝块 bigquery_options 时出错
- swift - 选择图像导致 Swift 内存崩溃