Sign in to follow this  
Leigon

Object destructor problems in template class.

Recommended Posts

Hi, I have a problem with a class and making copy's of pointers within that class.
template <class IT, class T>
T* Dictionary<IT,T>::get(IT index)
{
	int returnIndex;
	if((returnIndex = lIndex.indexOf(index)) != -1)
		return lData[returnIndex];
	return NULL;
}
template <class T>
int List<T>::indexOf(T object)
{
	for(int i = 0; i < (int)data.size(); i++)
	{
		if(data.at(i) == object)
			return i;
	}
	return -1;
}
class Refrance
{
	char * sName;
	...
}
sName in indexOf() and get() both point to the same memory location, so if I call "delete[] sName;" in the destructor I get a crash on return NULL in the dictionary... Do you guys have any idea no how to fix this small isue?

Share this post


Link to post
Share on other sites
Could you show a little more code, please. Especial the class declaration of your dict could be very interesting.
And by the way:
Couldn't you change int List<T>::indexOf(T object) to int List<T>::indexOf(const T &object) to avoid a unnecessary copying?

Kimmi

Share this post


Link to post
Share on other sites
It's good to have example reduced to minimal code showing the problem. But now you have reduced it too much. So as there seems to be no links between these threse classes, I'll just shoot comment what I see.

1. Why use char* instead of std::string?
2. C++ casts (static_cast<>) is better than C casts ((int)). Reason is, they stand out better, since one should aim for program with very few casts.
3. What is the meaning for prefix of l (in lIndex and lData)? Also I havent seen s prefix for class members before.
4. If you are doing char* manipulation by hand, have you implemented copy constructor, assignment operator aswell?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this