Archived

This topic is now archived and is closed to further replies.

Liquid

void pointers and destructors

Recommended Posts

I''m working on a resource management system for a project, and have a question. If I have something like void* pObject=new MyClass; and then sometime later called delete pObject; what would happen? Would MyClass''s destructor get called, and if not, what would happen? Is there anyway I can keep the generality (templates won''t work, nor can all of the classes inherit from some common base class) while still ensuring the object''s destructor is called? thanks in advance, Liquid

Share this post


Link to post
Share on other sites
Nope, deleting a void* won't call the destructor (and it might not free the memory correctly). Why wont templates work? Can't you just have something like:


class SomeBase
{
virtual ~SomeBase() = 0;
//blah...
}

template<class T>
struct Holder : public SomeBase
{
Holder(T* p) : m_ptr(t)
{}
~Holder()
{
delete m_ptr;
}

T* m_ptr;
}


Then use it like this:


SomeBase* pObject= new Holder<MyClass>(new MyClass);

// etc..

delete pObject;


Edited by - Wilka on October 29, 2000 2:05:56 PM

Share this post


Link to post
Share on other sites