且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在新的[]的值初始化中使用变量

更新时间:2023-11-25 08:07:34

可以使用变量,使用一个字符串,期望编译器将其分裂。

You can use a variable, but you can't use a string and expect the compiler to magically split it up.

const char* ip = initializer.c_str();
const char* foo = new char[4]{ip[0], ip[1], ip[2], ip[3]};

一旦你有一个可变长度,但是,这根本不工作。你只需要分配,然后使用 string :: copy 在单独的步骤中填充数组。

Once you have a variable length, though, this doesn't work at all. You just have to allocate and then use string::copy to fill the array in a separate step.