Hello
I am trying to understand when i need to use "delete".
I want to understand if i have dynamic declaration for object called X inside the class(named mycass) and then also i have dynamic deceleration in main for
myclass* first= new myclass();
From what i understand there is a need to free the memory after i finish using those class.
So i need to call delete in my main that free the object called first wright?and then it will go inside the class and will run destractorthat i am supposse to implement.am i wright?
main()
{.
.
.
delte myclass;
}
public :myclass
{
> myclass* first= new myclass();
give me one good reason to have that instead of simply myclass first;
Bjarne Stroustrup wrote:
Code that creates an object using new and then deletes it at the end of the same scope is ugly, error-prone, and inefficient.
But yes, for every new there must be the correspondent delete, or you are leaking memory.
When you delete, the destructor is invoked and the memory is freed.