指针,去甲肾上腺素的STL载体 [英] STL vector of pointer, derenferencin

查看:87
本文介绍了指针,去甲肾上腺素的STL载体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,代码人,

所以,我一直遇到这个问题,我有一些我构造的对象,而且我有一个指向这些对象的指针向量.然后,此向量传递给应该更改此对象的值的函数,但我不禁发现要执行此操作的视频.这样的东西

Hi there code people,

So, I''ve been having this problem, where I have some objects of a struct I made, and I have a vector of pointers to these objects. This vector is then, passed to a function who''s supposed to alter the values of this objects, but I can''t help to find the vode to do it. Something like this

typedef struct _foo
{
   int bar;
   void foo(int i) bar = i;

} foo;

foo var1(1), var2(2), var3(3);
vector<foo*> vec;
vec.pushback(var1);
vec.pushback(var2);
vec.pushback(var3);





然后,在其他cpp文件中,我正在做的是






Then, in some other cpp file, what I''m doing is


void foobar(vector<foo*> vec)
{
foo* aux;
for (int i=0;vec.size(), ++i)
{
   aux = vec[i];
   *(aux).bar = 1; //that''s where I don''t know the sintax, and I can''t find it
                   //anywhere. You probably understand what I mean with this pseudo
                   //sintax, I just want to alter the value in my struct, so to say,
                   //alter the value in the memory location of the object.
}
}




如果你们可以帮助我,我将非常感激.
固定的BBcode
Thankz;)




If you guys can help me with this, I would apreciate a lot.
BBcode fixed
Thankz ;)

推荐答案

Try
(*aux).bar = 1


aux->bar = 1;

顺便说一句,我在这里:
Try
(*aux).bar = 1

or
aux->bar = 1;

btw i think here:
vec.pushback(var1);
vec.pushback(var2);
vec.pushback(var3);


你的意思是


you mean

vec.pushback(&var1);
vec.pushback(&var2);
vec.pushback(&var3);


这篇关于指针,去甲肾上腺素的STL载体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆