I'd like to know how does the destructor behaves when a "new Class" is being passed to a function?
I have a code similar to this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class Truck;
template <typename VEHICLE>
void insertVehicle(VEHICLE* vehicle){
vehicle->setVehicle();
}
int main{
insertVehicle(new Truck);
}
/* Since I have several classes, I don't want to use this way:
int main{
Truck* truck = new Truck;
insertVehicle(truck);
delete truck
}
*/
Will any Truck object be created here ?
If yes, will it be deleted after passing to the function ?
Yes, there will be one Truck object created and it will not be deleted after passing to the function.
In order to understand that you just need to know how the new operator works.
It does allocate space for your truck object in memory and returns a pointer to that object to you.
All dynamically allocated memory does not get deleted unless you use the delete operator.
So you'll get problems with your code because this way the address of the new Truck will be lost and you wont be able to free the associated memory during runtime of your program.