Please read the following snippets:
It will crash when I attempt to delete b2 in the destructor. But I need to have a vector storing one object but is pointed by two or more vectors (like b1 and b2). I knew it is not the proper way to implement like this. Please could someone give any hint how to implement this without crashing?Code:class B { public: B() {} ~B() {} }; class A { public: A() {B* b1 = new B();B* b2 = b1;B* b3 = new B();B* b4 = b2;m_list.push_back(b1);m_list.push_back(b2);m_list.push_back(b3);m_list.push_back(b4);} ~A() {for (std::vector<B*>::size_type i = 0; i < m_list.size(); i++){if (m_list[i] != 0){delete m_list[i];//crash when deleting b2m_list[i] = 0;}}} private: std::vector<B*> m_list; }; void main() { A * a = new A(); delete a; }



2Likes
LinkBack URL
About LinkBacks



