Template Class Specialization Question

E

Erik Friis

Hi, I'm trying to create a class template like the following:

template <class Type>
class MyVector
{
MyVector();
~MyVector();

unsigned int MyCount;
Type* MyData;
}

where the destructor is defined like:

template <class Type>
MyVector<Type>::~MyVector()
{
delete [] MyData;
}

The problem is that when Type is something like a pointer to an object
then the destructor needs to delete each of those pointers before
deleting MyData, right? But if Type is an integral type such as
"unsigned int" then I don't need (or want) to do this....

So I thought that I might be able to use a specialization, but it
seems that there are too many cases to cover, no? Am I missing
something? Thanks for any direction!
 
J

John Harrison

Erik Friis said:
Hi, I'm trying to create a class template like the following:

template <class Type>
class MyVector
{
MyVector();
~MyVector();

unsigned int MyCount;
Type* MyData;
}

where the destructor is defined like:

template <class Type>
MyVector<Type>::~MyVector()
{
delete [] MyData;
}

The problem is that when Type is something like a pointer to an object
then the destructor needs to delete each of those pointers before
deleting MyData, right?

Well that is up to you. But I would say, wrong.

You are saying that MyVector should take ownership of any pointers added to
it. I would say that is poor design, just adding a pointer to a MyVector
doesn't mean that it is responsible for it's destruction. Consider

T* ptr = something();
if (ptr != 0)
{
MyVector<T*> vec_of_pointers;
vec_of_pointers.add(ptr);
...
} // ptr will be deleted here
ptr->something_else(); // *** CRASH ***, ptr has already been deleted

Do you consider that behaviour surprising? I do, but as I say it's up to
you.

What about this?

{
MyVector<char*> vec_of_pointers;
vec_of_pointers.add("watch me crash!!!");
} // crash here, attempting to delete a string literal

That behaviour would make me cross.
But if Type is an integral type such as
"unsigned int" then I don't need (or want) to do this....

So I thought that I might be able to use a specialization, but it
seems that there are too many cases to cover, no? Am I missing
something? Thanks for any direction!

You can use partial specialisation.

template <class Type>
class MyVector<Type*>
{
MyVector();
~MyVector();

unsigned int MyCount;
Type* MyData;
};

This is a specialised version of MyVector solely for pointers. Put this
class after your main MyVector class if you still think its a good idea.

john
 
J

John Harrison

John Harrison said:
Erik Friis said:
Hi, I'm trying to create a class template like the following:

template <class Type>
class MyVector
{
MyVector();
~MyVector();

unsigned int MyCount;
Type* MyData;
}

where the destructor is defined like:

template <class Type>
MyVector<Type>::~MyVector()
{
delete [] MyData;
}

The problem is that when Type is something like a pointer to an object
then the destructor needs to delete each of those pointers before
deleting MyData, right?

Well that is up to you. But I would say, wrong.

You are saying that MyVector should take ownership of any pointers added to
it. I would say that is poor design, just adding a pointer to a MyVector
doesn't mean that it is responsible for it's destruction.

The whole issue of pointers and ownership is important enough that it needs
it's own solution, quite separate from anything that you might do in
MyVector. Look up 'smart pointers' in your favourite C++ book.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top