Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;共享指针的向量。如果在向量外浇铸,它会改变向量中的指针吗?_C++_C++11_Vector_Casting_Shared Ptr - Fatal编程技术网

C++ c++;共享指针的向量。如果在向量外浇铸,它会改变向量中的指针吗?

C++ c++;共享指针的向量。如果在向量外浇铸,它会改变向量中的指针吗?,c++,c++11,vector,casting,shared-ptr,C++,C++11,Vector,Casting,Shared Ptr,我有一个基类base和两个派生类Child_a和Child_B。 当一个对象My_对象被实例化时(作为共享指针),我不知道它是Child_a还是Child_B,所以它被实例化为Base。 我将我的_对象推到基类型的共享指针的std::vector中。 稍后,当我知道我的\u对象属于哪个派生类时,我在我的\u对象上使用.reset()将其强制转换为派生类Child\u A。 我的问题是,向量中的My_对象是否也将被强制转换为Child_A? 如果没有,我怎么做? 谢谢 编辑: obj_array.

我有一个基类base和两个派生类Child_a和Child_B。 当一个对象My_对象被实例化时(作为共享指针),我不知道它是Child_a还是Child_B,所以它被实例化为Base。 我将我的_对象推到基类型的共享指针的std::vector中。 稍后,当我知道我的\u对象属于哪个派生类时,我在我的\u对象上使用.reset()将其强制转换为派生类Child\u A。 我的问题是,向量中的My_对象是否也将被强制转换为Child_A? 如果没有,我怎么做? 谢谢

编辑:

obj_array.push_back(std::shared_ptr<Base>(new Base());
container.push_back(obj_array[0]]);
obj_array[0].reset(Child_A());
obj_数组。推回(std::shared_ptr(new Base());
container.push_back(obj_数组[0]]);
obj_数组[0]。重置(子_A());
容器[0]将被强制转换为子容器吗? 我怎样才能把它投给Child_A

编辑以进一步澄清申请:
我想我对obj_数组的评论可能是共享的向量,ptr落入我的应用程序中,我希望有一个obj_数组>的主容器来保存所有对象。然后我有几个从容器Container1 Container2…来保存一些对象。我希望通过修改主容器contain来对任何对象进行主控呃,效果会广播到所有从属容器。在这个应用程序中,我想我可能只需要一个主向量
,并且有几个从属向量

我刚刚注意到您添加到问题中的代码。共享指针不会这样做

SharedPoints在“引用计数”方面是“共享的”,而不是在“数据共享”方面是“共享的”。“数据共享”在您的术语中是由..指针完成的。无论您想“共享”什么,通过指针引用它。然后,如果您更改它,每个人都会看到更新。但是您必须更改,而不是指向它的指针

也就是说,在这里,你想更新指针,你想让每个人都看到指针被更新。因此,你必须一个指针一个指针地握住指针

也就是说,与其保留
向量
,不如保留
向量

现在,当您想用新实例“全局替换”对象时,可以用另一个新的shptr替换指针持有的
shptr


如果你不喜欢原始指针,你甚至可能会使用
vector。如果你说的是
std::vector
,请展示一些代码,否则你的问题会让人困惑!基本上问题是,我有Foo*fooptr。我把fooptr放在一些容器中。然后我发现fooptr应该是Bar*。如何将fooptr改为Bar*f还是所有的容器?这里有一个与你的问题完全相同的副本,但描述得更好:下次请更具体一点。这三行代码告诉我的比你所有的文本都多——主要是因为你说了一些关于“投射我的对象”的奇怪事情。我认为你的答案比副本解释得更好。另外一点,obj_数组不必是指针指向指针的向量。它可以是向量。如果我错了,请纠正我。谢谢。请重新阅读我的答案。而不是徒劳地问你“moar detailz”,我只是简单地解释了不同的选择和后果。谢谢你的例子。付出了巨大的努力。
// obj_array and container are a vector<shared_ptr<Base>*>
// or a vector<shared_ptr<shared_ptr<Base>>>

obj_array.push_back(new std::shared_ptr<Base>(new Base()); // note the 'new'
container.push_back(obj_array[0]]);

(*obj_array[0]) .reset(Child_A()); // note the '*'

obj_array[0] -> reset(Child_A()); // or, just in short
vector<Base*> vector;
vector.resize( 10 );

///// .... later ....

Base* olditem = vector[ 4 ];
Base* newitem = new Bar();

bool iWillDeleteTheOld = well_somehow_decide();

vector[4] = newitem;
moduleB->updateAfterReplace(olditem, newitem,  iWillDeleteTheOld);
modulec->updateAfterReplace(olditem, newitem,  iWillDeleteTheOld);

if(iWillDeleteTheOld)
   delete olditem;

///// .... later ....

for( ... idx ...)
    delete vector[idx];

vector.resize(0);
vector<shared_ptr<Base>> vector;
vector.resize( 10 );

///// .... later ....

sharedptr<Base> olditem = vector[4];
sharedptr<Base> newitem = new Bar();

vector[4].reset( newitem ); // <- THE LINE

moduleB->updateAfterReplace(olditem, newitem);
modulec->updateAfterReplace(olditem, newitem);

///// .... later ....

vector.resize(0);
vector<Base**> vector;
for(int i = 0; i<10; ++i)
   vector.push( new Base* );

///// .... later ....

Base* olditem = * vector[4]; // note the dereference
Base* newitem = new Bar();

bool iWillDeleteTheOld = well_somehow_decide();

* vector[4] = newitem; // note the dereference

if(iWillDeleteTheOld)
   delete olditem;

///// .... later ....

for( ... idx ...)
{
    delete * vector[idx];  // delete the object
    delete vector[idx]; // delete the pointer
}

vector.resize(0);
vector<sharedptr<sharedptr<Base>>> vector;
for(int i = 0; i<10; ++i)
   vector.push( new sharedptr<Base> );

///// .... later ....

(* vector[4] ).reset( new Bar() ); // note the dereference

///// .... later ....

vector.resize(0);
shared_ptr<Foo> first = new Foo(1);
shared_ptr<Foo> second = first;
shared_ptr<Foo> third = second;

// now first == Foo#1   \
// now second == Foo#1  | refcount = 3
// now third == Foo#1   /

third.reset( new Bar(2) );

// now first == Foo#1   \ refcount = 2
// now second == Foo#1  /
// now third == Bar#2   - refcount = 1

second.reset( new Asdf(3) );

// now first == Foo#1   - refcount = 1
// now second == Asdf#3 - refcount = 1
// now third == Bar#2   - refcount = 1

first.reset( third );

// now first == Bar#2                   \
// now second == Asdf#3 - refcount = 1  | - refcount = 2
// now third == Bar#2                   /
// and Foo#1 gets deleted