poo*_*ent 1 c++ arrays variables member
我在设置数组大小时遇到问题.在我的代码中,我有:
class Test {
    public:
       ....//Functions
    private:
      string name[];
};
Test() {
   //heres where i want to declare the size of the array
}
这可能吗?
不.但你可以使用字符串向量代替:
private:
  std::vector<std::string> name;
然后在你的构造函数中:
Test()
    : name(sizeOfTheArray)
{
}
矢量将根据您指定的字符串数量进行调整.这意味着字符串的所有内存将立即分配.您可以根据需要更改数组的大小,但没有必要说明.因此,您可以获得使用动态分配的数组的所有好处,然后使用一些,没有缺点.