Static member variable pointer.
-
Just a quick question, when I have a static member pointer var.. class Poo { static Poo * _poo } -- implemented Poo * Poo::_poo = new Poo(); Poo::Poo()...... Do I look after the destruction of the pointer or does that happen automatically Cheers Asim Hussain e: asim@jawache.net w: www.jawache.net
-
Just a quick question, when I have a static member pointer var.. class Poo { static Poo * _poo } -- implemented Poo * Poo::_poo = new Poo(); Poo::Poo()...... Do I look after the destruction of the pointer or does that happen automatically Cheers Asim Hussain e: asim@jawache.net w: www.jawache.net
Jawache wrote: Do I look after the destruction of the pointer or does that happen automatically Typically anytime you use the new operator to allocate memory on the heap you need to handle the deletion of it when you are through. Nick Parker
May your glass be ever full. May the roof over your head be always strong. And may you be in heaven half an hour before the devil knows you’re dead. - Irish Blessing
-
Just a quick question, when I have a static member pointer var.. class Poo { static Poo * _poo } -- implemented Poo * Poo::_poo = new Poo(); Poo::Poo()...... Do I look after the destruction of the pointer or does that happen automatically Cheers Asim Hussain e: asim@jawache.net w: www.jawache.net
Here are some points to consider: 1. If the value of _poo will never change, in other words, it will never be reassigned to a different object, then:
-
It doesn't really need to be deleted -- the program will clean it up when it exits. However, if you run the program in the DevStudio debugger, you'll get a message about a memory block that wasn't freed, which for this case is harmless, but it's better to not have it so that it's not confused with real leaks.
-
You don't really need to make _poo a pointer to Poo. You can make it a Poo object directly:
class Poo
{
static Poo _poo;
};Poo Poo::_poo;
2. If the value of _poo (as you have it) will change, then you will obviously need to free the memory that it points to before reassigning it. Otherwise you'll have memory leaks. Regards, Alvaro
Well done is better than well said. -- Benjamin Franklin (I actually prefer medium-well.)
-