||July 28, 2009 17:50
autoPtr, derived classes and memory leak
i am trying to understand generic programming concepts used in OpenFOAM, and have a basic question in C++.
suppose I have a "employee" base class which has virtual functions, and two derived classes:
I want to have a pointer to the employee base class called myEmployee, and be able to choose at runtime wether it points to a employeeExportA or employeeExportB object. I tried this code:
if (inputString == "A")
myEmployee = auto_ptr<employeeExportA>(new employeeExportA(4,5));
if (inputString == "B")
myEmployee = auto_ptr<employeeExportB>(new employeeExportB(4,5));;
My program compiles, but I would like to know, since the auto_ptr myEmployee has been created with the base class, will it be able to clean all the memory created by the "new" operator applied to a derived class, or will it only clean the part of the object in common with the base and let the remaining leaking ?