Object destructor problems in template class.

Started by
1 comment, last by Codarki 15 years, 1 month ago
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?
Advertisement
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
A complicate solution may indicate a not understood problem.


[twitter]KimKulling[/twitter]
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?

This topic is closed to new replies.

Advertisement