I have the following code and I’m wondering if that delete b is necessary here ?
Will my operating system automatically clear that area of memory allocated ?
class A
{
B *b;
A()
{
b = new B();
}
~A()
{
delete b;
}
};
Many thanks.
Yes, you have to
deleteevery object created withnewthat you own. In this very case it looks likeclass Aowns that very instance ofclass Band is responsible for callingdelete.You’ll be much better off using a smart pointer for managing
class Binstance lifetime. Also note that you have to either implement or prohibit assignment operator and copy constructor inclass Ato prevent shallow copying the object which will cause you much trouble.