I am currently trying to fix a few weaknesses in our code base by introducing the use of smart pointers. The code base is very large, and interlinked like a spider who's had one to many coffee's.
I was wondering if people had tried the before and what their approach was.
My first step has been to typedef classes, as follows.
#ifndef USE_SMART_POINTERS
#define USE_SMART_POINTERS 0
#endif
#if USE_SMART_POINTERS == 1
#include <boost/smart_ptr.hpp>
#endif
namespace ProductX
{
// forward decleration
class CTObject;
//typedefs
#if USE_SMART_POINTERS == 1
typedef boost::shared_ptr<CTObject> CTObjectPtr;
#else
typedef CTObject* CObjectPtr;
#endif
}
Now I realise this will lead to a wealth of compile areas, things like
CTObjectPtr i = NULL;
Will completly bork when smart pointers are enabled.
I was wondering if there was anything I could do at this early stage to reduce the mass of compile errors, or is it as I suspect just take things on a case by case basis.
Cheers Rich
Don't do this: the typedefs I mean.
Presumably the old code has at least some delete calls in it? Which would certainly fail in the case of a smart pointer.
Smart pointer certain things or not, i.e. chase a specific instance through the code base. Make it work, then move on. Good Luck!
Instead of trying to introduce smart pointers everywhere you could use the Boehm-Demers-Weiser garbage collector and leave your code base intact.
It will also take care of cyclic references.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With