normally the object new bar()
will be automatically destroyed after exiting the scope why I have to do a delete pBar
then ?
class bar{
public:
bar(){}
//...
};
class foo{
public:
foo(){}
void createBar(){
bar* pBar = new bar();
// stuff..
delete pBar;
}// why should I use delete and the pBar should be delted after the end of the scope ?
};
also if in the function createBar()
I add pBar
pointer to a vector of another class. the vector has only point to the pBar
(not owner). in this case where do I put the delete ? because if I keep it where it is it will delete the pointers then my manager vector will point to nothing ..
if I use the delete
inside the manager class, so I will delete an object inside a non-owner class ?!
PS: I know that I can use smart pointer, but I just want to understand this point
void createBar(){
bar* pBar = new bar();
// stuff
manager::add(pBar);
delete pBar;
}
class manager{
public:
manager(){}
void add(bar* p){
vFoo.push_back(p);
}
private:
std::vector<bar*> vFoo;
}