Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个结构初始化:
nodeBuf = new Node[num](); //num is some number of different instances of this struct
我知道这会创建我的结构的 num 个实例。但我不确定如何访问每个实例。
我试过nodeBuf [num],不工作
如果num > 0只有nodeBuf[0]直到nodeBuf[num -1]有效。如果num == 0则不允许通过索引运算符访问元素(= UB)。
num > 0
nodeBuf[0]
nodeBuf[num -1]
num == 0
样本:
结构节点 { 标准::字符串名称; }; 无效 f() { 整数 = 4; Node* nodeBuf = new Node[num]; nodeBuf[1].name = "测试"; 删除[] 节点缓冲区; }