Skip to main content
gamedev.net
🔒 Locked

void pointers and destructors

Started by Liquid 1 reply 300+ views
Original Post
Liquid
Liquid
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
Dave007
Dave007
Yes, it''ll.
--------Dave[ Math Studio ] A Computer Algebra System
Wilka
Wilka
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.