R
Rob
I have a vector of a class type and when I create an object inside a
function and return that and add it to the vector, it doesn't properly
keep the data inside it. So I have a few questions:
1. Is this because it loses scope since it wasn't created with "new"?
2. If I do create it with new, but the vector holds objects not
pointers, will the vector's "delete" operator function still handle
deleting all those pointers?
CODE 1:
//Doing without "new"
SomeClass create(string name)
{
return SomeClass( name );
}
int main()
{
std::vector<SomeClass> p;
p.push_back( create( "Fred" ) );
}
CODE 2:
//Doing with "new"
SomeClass create(string name)
{
return *( new SomeClass( name ) );
}
int main()
{
//Will calling "delete p" also delete all the pointers created
for the data it holds?
std::vector<SomeClass> p;
p.push_back( create( "Fred" ) );
}
function and return that and add it to the vector, it doesn't properly
keep the data inside it. So I have a few questions:
1. Is this because it loses scope since it wasn't created with "new"?
2. If I do create it with new, but the vector holds objects not
pointers, will the vector's "delete" operator function still handle
deleting all those pointers?
CODE 1:
//Doing without "new"
SomeClass create(string name)
{
return SomeClass( name );
}
int main()
{
std::vector<SomeClass> p;
p.push_back( create( "Fred" ) );
}
CODE 2:
//Doing with "new"
SomeClass create(string name)
{
return *( new SomeClass( name ) );
}
int main()
{
//Will calling "delete p" also delete all the pointers created
for the data it holds?
std::vector<SomeClass> p;
p.push_back( create( "Fred" ) );
}