指向void函数中类的指针 我正在学习C++中的类/继承/指针如何工作,并编码如下。p>
我有一个类单元声明如下:指向void函数中类的指针 我正在学习C++中的类/继承/指针如何工作,并编码如下。p>,c++,class,pointers,inheritance,C++,Class,Pointers,Inheritance,我有一个类单元声明如下: class unit{ public: int locationX,locationY; //genotype (does not change) float agility, build,size,stamina, aggression; //phenotype (changes based on genotype and stimuli) float speed, streng
class unit{
public:
int locationX,locationY;
//genotype (does not change)
float agility, build,size,stamina, aggression;
//phenotype (changes based on genotype and stimuli)
float speed, strength, hunger;
};
当我创建一个新实例以传递到一个void函数(分别如下)时,内存尚未分配
实例
unit **units = 0;
void initialize(grid *grids, unit **units /*, plant **plants, predator **predators */);
空洞函数原型
unit **units = 0;
void initialize(grid *grids, unit **units /*, plant **plants, predator **predators */);
使用一些参数在void函数中分配内存:
void initialize(grid *grids, unit **units,plant **plants,predator **predators)
{
units = new unit*[int(((grids->gridHeight)*(grids->gridWidth)*(grids->gridDivision))/20)];
for(register int i = 0; i<int(((grids->gridHeight)*(grids->gridWidth)*(grids->gridDivision))/20); i++)
{
units[i] = new unit;
units[i]->hunger = 5;
units[i]->locationX = (rand()%((grids->gridWidth)-0));
units[i]->locationY = (rand()%((grids->gridHeight)-0));
//etc, etc
}
}
注意:我只对在单位类下声明的单位变量有问题。环境变量很好。另外两个(植物和捕食者)类似于单位,所以如果这是固定的,我可以固定其他单位
第二注:主要功能如下(相关部分):
intmain()
{
unit**units=0;//您通过值将units
传递到函数。这意味着函数中的units
指针以调用代码中指针的副本开始。在函数中,您为本地units
变量(即一些新创建对象的地址)分配一个新值。然后,当函数终止时,局部变量将超出作用域,它指向的对象将丢失。调用代码中的指针永远不会被修改,并且对此一无所知
请改为通过引用传递:
void initialize(grid *grids, unit ** &units)
您将“units”作为堆栈上的“unit**”类型传递。当您为变量“units”分配新值时,您将影响函数的局部变量。如果您希望调用代码更新其变量“units”,则必须通过指针或引用传递它
解决此问题的最简单方法是将函数签名更改为:
void initialize(grid *grids, unit **&units, etc.. );
这是将变量单位作为指向单位指针的指针的引用传递。在方法内部初始化指针(或数组)a*a
是修改变量a
(在这种情况下是指针/数组)的一种特殊情况。修改方法中的变量可以通过传递指向它的指针或对它的引用来完成。因此,如果变量已经是指针,则以下两个选项是
通过将指针传递给指针a
:
void init(A * *a)
{
*a = new A(); // or "new A[n]" if "a" is an array
}
void main()
{
A * a;
init(&a);
}
或将引用传递给指针:
void init(A * &a)
{
a = new A(); // or "new A[n]" if "a" is an array
}
void main()
{
A * a;
init(a);
}
因此,在您的例子中,a
是一个unit*
的数组(顺便说一下,我认为这是一个糟糕的设计,最好只使用unit
的数组,并将您从这个循环中解救出来,以便分别动态分配所有单元),因此基本上a
属于unit**
类型,因此您的a
实际上是unit*
类型。使用第二种方法将导致:
void init(unit* * &units)
但正如我所说的,最好使用单元数组
,整个代码如下所示:
void initialize(unit * &units /* etc */)
{
// note that I use unit instead of unit* here
units = new unit[n];
for(register int i = 0; i<n; i++)
{
// note that I remove the "new unit;" here
// note that I use "." instead of "->"
units[i].hunger = 5;
units[i].locationX = (rand()%((grids->gridWidth)-0));
units[i].locationY = (rand()%((grids->gridHeight)-0));
//etc, etc
}
}
void main()
{
unit * units;
initialize(units);
}
void初始化(单位*&units/*etc*/)
{
//请注意,我在这里使用的是unit,而不是unit*
单位=新单位[n];
对于(寄存器int i=0;igridWidth)-0));
单位[i]。位置=(rand()%((网格->网格高度)-0);
//等等等等
}
}
void main()
{
单位*单位;
初始化(单位);
}
除了按预期工作外,它还可以节省一些时间overhead@Beta:谢谢你的修复。现在可以了!“将变量单位作为指向单位指针的指针的引用传递”:所以我认为您的意思也是unit**&units
,而不是units*&units
:-)哦,是的!谢谢(原来是文本格式问题)