J
Jeffrey Walton
Hi All,
I'm working with some C++ code that performs a lot of heap allocations
on typef'd structs, structs, and classes, but does not ensure cleanup.
Many of the classes and structs hide their assignment operator.
I attempted to write a template class to ensure cleanup, but am
receiving the error, "operator= could not be generated".
Two questions. (1) I'm dealting with pointers and not objects. So why
is an assignmnet operator needed? (2) I know the void* hack is
bullshit. How do I accomplish my goals of the delete, and then set the
pointer to NULL.
Thanks,
Jeff
template <class T>
PtrTypeCleanup<T>:
trTypeCleanup(T*& ptr)
: m_ptr(NULL)
{
// Most types have their assignment operator hidden.
// Cast so that we can set the bastard to NULL.
reinterpret_cast<void*>(m_ptr) = reinterpret_cast<void*>(ptr);
}
template <class T>
PtrTypeCleanup<T>::~PtrTypeCleanup(void)
{
if(m_ptr)
{
// We must delete through the type
delete m_ptr;
// Most types have their assignment operator hidden.
// Cast so that we can set the bastard to NULL.
// void* p = reinterpret_cast<void*>(m_ptr);
// p = NULL;
}
}
template <class T>
void PtrTypeCleanup<T>::Release(void)
{
// Most types have their assignment operator hidden.
// Cast so that we can set the bastard to NULL.
// void* p = reinterpret_cast<void*>(m_ptr);
// p = NULL;
}
template <class T>
T*& PtrTypeCleanup<T>:
perator=(const T*& rhs)
{
// No self assignment
if(this == &rhs) { return *this; }
reinterpret_cast<void*>(m_ptr) =
reinterpret_cast<void*>(rhs.m_ptr);
}
I'm working with some C++ code that performs a lot of heap allocations
on typef'd structs, structs, and classes, but does not ensure cleanup.
Many of the classes and structs hide their assignment operator.
I attempted to write a template class to ensure cleanup, but am
receiving the error, "operator= could not be generated".
Two questions. (1) I'm dealting with pointers and not objects. So why
is an assignmnet operator needed? (2) I know the void* hack is
bullshit. How do I accomplish my goals of the delete, and then set the
pointer to NULL.
Thanks,
Jeff
template <class T>
PtrTypeCleanup<T>:
: m_ptr(NULL)
{
// Most types have their assignment operator hidden.
// Cast so that we can set the bastard to NULL.
reinterpret_cast<void*>(m_ptr) = reinterpret_cast<void*>(ptr);
}
template <class T>
PtrTypeCleanup<T>::~PtrTypeCleanup(void)
{
if(m_ptr)
{
// We must delete through the type
delete m_ptr;
// Most types have their assignment operator hidden.
// Cast so that we can set the bastard to NULL.
// void* p = reinterpret_cast<void*>(m_ptr);
// p = NULL;
}
}
template <class T>
void PtrTypeCleanup<T>::Release(void)
{
// Most types have their assignment operator hidden.
// Cast so that we can set the bastard to NULL.
// void* p = reinterpret_cast<void*>(m_ptr);
// p = NULL;
}
template <class T>
T*& PtrTypeCleanup<T>:
{
// No self assignment
if(this == &rhs) { return *this; }
reinterpret_cast<void*>(m_ptr) =
reinterpret_cast<void*>(rhs.m_ptr);
}