![](/img/trans.png)
[英]Initialize a vector of classes that have member pointers to another class' member variables
[英]Pointers as member variables to a class/dynamic memory in classes
假设我们有以下内容构成一个10 x 10二维数组并将第一个单元格设置为100:
class twoD{
int **p = new int*[10];
public:
twoD(){
*p = new int[10];
p[0][0] = 100;
}
};
为什么以下情况不正确,为什么可以
class twoD{
int **p = new int*[10];
*p = new int[10];
public:
twoD(){
p[0][0] = 100;
}
};
因为在第二种情况下,“ * p = new int [10];” 不是变量声明,因此不能在类定义中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.