I’m changing some legacy code so that is uses smart pointers.
Our projects pre-date boost and so we have an existing intrusive smart pointer class (see below for API).
I want to remove explicit calls to delete for these types, so what is the best way to find them?
For example:
class A : public RefCount // RefCount added to manage lifetime
{
// ...
};
// typedef A * APtr; => Previous implementation
typedef SPtr<A> APtr;
void deleteObjects (APtr a)
{
delete a; // Want to find this (ideally with compile error)
}
All types that are to be used with the smart pointer derive from a base class that keeps a reference count, and it is this class that should be responsible for deleting the object when the count reaches zero.
I’ve added what I’m doing today as an answer, however, the approach doesn’t work for calls to delete in hierarchies. I am looking for a complete solution that catches any explicit call to delete (other than the one from the RefCount member).
The smart pointer class has the following members:
template <typename B>
class SPtr
{
public:
// Ctor
SPtr () throw ();
SPtr (B * b) throw ();
SPtr (const SPtr & bptr) throw ();
// Copy Asgn
SPtr & operator = (B * b);
SPtr & operator = (const SPtr & bptr);
// Dtor
~SPtr () throw ();
// Access
bool isSet () const;
B * pointer () const;
B * operator -> () const;
B & operator * () const;
operator void const * () const;
};
Get rid of
operator void const * () const;in your smart pointer class and that should do it. Implicit conversions from smart pointer to raw pointer generally hide so many problems it’s not worth implementing.Even with the conversion operator I still got a warning in g++ with the code as posted.