English 中文(简体)
2. 如何在C++的病媒内检索物体
原标题:how to retrieve an object inside a vector in c++

I m a C++ noob programmer... (I usually use Java) and now I ve got some problems with pointers, references and vectors...

页: 1 目标,从vector<Object> Object and for simplicity i 我想在另一个物体中储存his reference, I need to edit some criteria within it many倍

这是迄今为止撰写的法典:

Object myObject = getMyObject(id, objects); 

www.un.org/chinese/sc/presidency.asp 右铭object, 右边id,并以<代码>returnbject[i]将其退回

但是,如果我理解正确的话,这种方法就重复了目标! 页: 1 是否正确?

我怎么办?

非常感谢!

最佳回答

你是正确的。 你们应该做的是,恢复工作:

Object & getMyObject(int id, std::vector<Object> & objects)
{
    // ... find ...
    return objects[i];
}

之后,在您的用户代码中将这一点与另一个参考资料联系起来:

Object & theobject = getMyObject(12, all_objects);

现在,你通过参考<条码>正文/代码”直接修改其中包含的内容。

如果您不希望/要求修改标的,只检查标的,请参见(以及集装箱)<条码>。

Object const & getMyObject(int id, std::vector<Object> const & objects);
问题回答

是的,你需要使用点或参考,否则你显示的线将称作<代码>的复印件。 目标

您可以制定<条码>目标/代码> 返回代码,然后请您向该物体发出点,并指定另一条<代码>。 反对*。

或者,界定<代码>my 目标作为参考:Object& myObject

你们必须保证,你回来的物体是动态分配的,而不是作为地方变量分配的:getMyObject(),因为否则,你会受到违反。

你们还必须保证该物体的自由,以防止记忆泄露。

如果你不想重复该物体,那么你需要参考或点。 问题在于,参考人或点人很容易变成无效。 如果你不想发生这种情况,那么你就应当把点子放在你身上。 具体来说,你很可能要分享。

std::shared_ptr<Object> getMyObject(foo id,
    const std::vector<std::shared_ptr<Object>> & objects)
{
    // ... find object
    ...
    return objects[i];
}
...
std::vector<std::shared_ptr<Object>> objects;
objects.emplace_back(new Object(parameters));
...
std::shared_ptr<Object> x = getMyObject(id, objects);

铭记STL总是复制,除非另有说明。 然而,(......)的病媒和操作者都交回了参考书。

你们的需要

Object& myObject = objects.at(id);

如果你想要改变所收集的物品。

Object myObject = objects.at(id);

页: 1

相反,集装箱内原始物体的储存点。

Object* obj1 = new Object();
Object* obj2 = new Object();

vector<Object*> myVector;
myVector.push_back(obj1);
myVector.push_back(obj2);

Object* tmpObj = myVector.at(0);




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签