You do realize that words is local to the Dictionary constructor, and the memory you allocate for it and the memory it allocates to hold your stuff is going to be leaked as soon as the constructor is left?
So I have to const_cast it.
Perhaps it would be better to just respect the const.
no, you do have to const-cast it,a and there is no error in it! As long as you don't change the const item, you can const_cast it as much as you want:
1 2 3 4 5 6 7 8 9 10 11
Dictionary::Dictionary(const std::vector<constchar*> &list)
{
int i,Size;
vector<char*>* words = new vector <char*>;
Size=(int)list.size();
for(i=0;i<Size;i++)
words->push_back(const_cast<char*>(list[i])); // no error, not changing the item
//just casting it to match the other side of the equation
//(happening inside the push_back() function)
root=new Dict_Node();
}
Dictionary::Dictionary(const std::vector<constchar*> &list)
{
int i,Size;
vector<constchar*>* words = new vector <constchar*>;
Size=(int)list.size();
for(i=0;i<Size;i++)
words->push_back(list[i]); // no error, not changing the item
//just casting it to match the other side of the equation
//(happening inside the push_back() function)
root=new Dict_Node();
}
Yes, that is correct. You shouldn't ever try to hold data that was originally const char* in a char* container, since it might be illegally changed in the future. Now that the vector holds const char*, the compiler will stop you from making illegal changes.