Can an object delete itself?

Started by
13 comments, last by DanG 21 years, 7 months ago
This hasn''t come up yet.

If you want to be sure you don''t get it wrong, and also want
to be exception-safe, it''s good to have a "smart pointer"
that takes care of adding and subtracting reference counts.


  template <typename T>class RefPtr    {    private:    T *m_pObject;    public:    RefPtr(T *pObject) : m_pObject(pObject)        { pObject->AddRef(); }    RefPtr(const RefPtr &Other) : m_pObject(Other.m_pObject)        { m_pObject->AddRef(); }    ~RefPtr(void)        // Release() returns true if ref count drops to 0        { if (m_pObject->Release()) delete m_pObject; }    const RefPtr &operator=(const RefPtr &Other)        {        if (m_pObject->Release()) delete m_pObject;        m_pObject = Other.m_pObject;        m_pObject->AddRef();        return *this;        }    // end if    T *operator->(void) const        { return m_pObject; }    T &operator*(void) const        { return *m_pObject; }    };// end  


This class is by no means complete. I leave that as an
exercise to the reader (i.e. you) to fix the holes.

(Hint: null-pointer (I mean 0) and self-assignment...etc)


Kami no Itte ga ore ni zettai naru!
神はサイコロを振らない!
Advertisement
quote:Original post by daerid
Yes, I know. I cheated.

Not only did you cheat, you didn''t allocate a foo object. An object lifetime does not begin until the ctor has completed, and in your code no ctor for foo has been called. Basically, all you''ve achieved is to lie to the compiler, by invoking reinterpret_cast and saying "here''s some storage which is layout compatible with and contains a valid foo instance" (which, of course, it doesn''t). Any calls to foo member functions via f will invoke undefined behaviour. Quite frankly, I wouldn''t be surprised if you''ve had a few demons fly out of your nostrils whilst running that code.
Well that''s true, free() fails if the object wasn''t created dynamically. But my point was that Beer_Hunter''s example also does not work because you can use malloc() to create the object.

But I see what you''re saying -- you meant using create() basically guarantees the object is created dynamically instead of just having a static instance of it.

~CGameProgrammer( );

~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
quote:Original post by CGameProgrammer
Well that''s true, free() fails if the object wasn''t created dynamically. But my point was that Beer_Hunter''s example also does not work because you can use malloc() to create the object.
I was trying to prevent mistakes - not deliberate stupidity
quote:Original post by CGameProgrammer
But my point was that Beer_Hunter's example also does not work because you can use malloc() to create the object.

No you cannot. I've just told you that you haven't created the object with malloc(). Object creation is a 2-step process. First, the storage is allocated. Second the ctor is executed. You've only achieved the first, so the object has *not* been created. If you cannot gain access to the ctor of the object, then you cannot create an instance of that object. Period.

You could create another object that is layout compatible and contains the correct representation, but the effort it requires to do that should tell you that you're doing something wrong. Besides, the point of such techniques is to make it easy for the developer to get things right. It's not possible to prevent people from writing code with malicious intent, other than to fire them.

[edited by - SabreMan on August 31, 2002 5:28:32 AM]

This topic is closed to new replies.

Advertisement