boost::shared_ptr vs. NULL
I'm running Boost 1.32.0 under MinGW, so NULL is 0 rather than (void*)0. While I understand what smart pointers are all about, this is the first time I've tried converting code with conventional pointers to use smart pointers. My original application had class data members such as Node *mParent; which were initialized to NULL in the class constructor initialization lists. Later, the data members were assigned pointers to real Node objects. In particular, the application needs at various times to detect whether the data member is NULL or a "real" node and to occassionally set real nodes to NULL. OK, now I'm trying to convert to shared_ptrs. So, I have a typedef for notation convenience: typedef boost::shared_ptr<Node> NodePtr; The class data member declaration now looks like NodePtr mParent; but, how do I initialize it to NULL? In the smart pointer world *is* there such a thing as a generic NULL that can be assigned to all smart pointers. Or, will I need something like NodePrt NullNodePtr; // never initialized then assign mParent = NullNodePtr; This would mean that I would been a NullXPtr for every smart pointer type X I have. If that's the way it must be, then OK, but I would like to know how things are supposed to be done. Merrill
On Sat, 30 Jul 2005 21:55:56 -0400 (EDT), Merrill Cornish
I'm running Boost 1.32.0 under MinGW, so NULL is 0 rather than (void*)0.
While I understand what smart pointers are all about, this is the first time I've tried converting code with conventional pointers to use smart pointers. My original application had class data members such as
Node *mParent;
which were initialized to NULL in the class constructor initialization lists. Later, the data members were assigned pointers to real Node objects. In particular, the application needs at various times to detect whether the data member is NULL or a "real" node and to occassionally set real nodes to NULL.
OK, now I'm trying to convert to shared_ptrs. So, I have a typedef for notation convenience:
typedef boost::shared_ptr<Node> NodePtr;
The class data member declaration now looks like
NodePtr mParent;
but, how do I initialize it to NULL? In the smart pointer world *is* there such a thing as a generic NULL that can be assigned to all smart pointers. Or, will I need something like
A default constructed smart pointer is initialized to 0. You can get the raw pointer with operator get(): if (!mParent.get()) { ... uninitialized } To set it to 0, use reset(): mParent.reset(); -- Be seeing you.
participants (2)
-
Merrill Cornish
-
Thore Karlsen