或者你可以做一个析构函数
~A()
{
for(unsigned int i =0; i < vec.size(); ++i)
delete [] vec[i];
}
编辑
正如所指出的,您还需要进行复制和分配(如果您打算使用它们)
class A
{
public:
A& operator=(const A& other)
{
if(&other == this)
return *this;
DeepCopyFrom(other);
return *this;
}
A(const A& other)
{
DeepCopyFrom(other);
}
private:
void DeepCopyFrom(const A& other)
{
for(unsigned int i = 0; i < other.vec.size(); ++i)
{
char* buff = new char[strlen(other.vec[i])];
memcpy(buff, other.vec[i], strlen(other.vec[i]));
}
}
std::vector<char*> vec;
};
更多关于深度复制的主题以及为什么在这里需要它
http://www.learncpp.com/cpp-tutorial/912-shallow-vs-deep-copying/