使用指针数组作为参数传递给方法 [英] Using array of pointers as parameter to method
问题描述
我必须使用指针数组对象,我也必须把它作为参数的方法。然而,要做到这一点的方式逃避我。这里是我使用的数组的元素的初始化的方法。当我提领他们在主,他们的数据是不正确的(它们包含的内存地址)。什么是正确的方法是什么?也许它是假的方式提领我他们?
I have to use an array of pointers to Objects and I must also pass it as parameter to methods. However the way to do this eludes me. Here is the method I use for the initialization of the elements of the array. When I dereference them in main, their data are not correct (they contain memory addresses). What is the correct way? Might it be false the way I dereference them?
void createSquares(Square* squareArray[]){
PropertySquare PropertySquare1(1,"purple",120);
PropertySquare PropertySquare2(2,"purple",170);
squareArray[1] = &PropertySquare1;
squareArray[2] = &PropertySquare2;
.
.
.
}
在主要的:
Square *allSquares[22] ;
createSquares(allSquares);
cout<<"ID is: "<<allSquares[1]->getID()<<endl;
cin.get();
正如我所说的ID是终于内存地址。
As I said the ID is finally a memory address.
更新基于答案:
我已经试过这一点,它不工作作为well.It当务之急是我使用多态。
I have tried this and it does not work as well.It is imperative for me to use polymorphism.
vector<Square*> allSquares;
createSquares(allSquares);
void createSquares(vector<Square*> &squares){
PropertySquare PropertySquare1(1,"purple",120);
PropertySquare PropertySquare2(2,"purple",170);
squares.push_back(&PropertySquare1);
squares.push_back(&PropertySquare2);
}
在主:
for (vector<Square*>::iterator it=allSquares.begin(); it!=allSquares.end();it++){
it->
}
它不允许我使用广场的虚函数,因为它是抽象的。
任何建议?
It does not allow me to use the virtual functions of Square since it is abstract. Any suggestion?
推荐答案
你做的一切都是不好的。这是棘手弄清楚哪里开始,所以让我开始在年底和present正确方法:
Everything you're doing is Not Good. It's tricky to figure out where to begin, so let me start at the end and present The Right Way:
typedef std::unique_ptr<Square> square_ptr;
void createSquares(std::vector<square_ptr> & v)
{
v.emplace_back(new PropertySquare1(1,"purple",120));
v.emplace_back(new PropertySquare1(2,"green",370));
// ...
}
int main()
{
std::vector<square_ptr> allSquares;
createSquares(allSquares);
for (const auto & p : allSquares)
std::cout << "ID is: " << p->getID() << std::endl;
}
现在打破你的问题:
Now to break down your problems:
首先,你要存储局部变量的指针。这些局部变量死在函数范围的结束,指针变成晃来晃去。解引用是程序错误。
First off, you are storing the pointers of local variables. Those local variables die at the end of the function scope, and the pointers become dangling. Dereferencing is a program error.
第二,要解决这个问题,您应该创建的动态的对象: squareArray [1] =新PropertySquare1(1,紫,120);
然而,这是有问题的,也。有人会来清理这些对象!你可以遍历每个元素的数组,并调用删除
。
Second, to fix this, you should create dynamic objects: squareArray[1] = new PropertySquare1(1,"purple",120);
However, that is problematic, too. Someone will have to clean up those objects! You could iterate over the array and call delete
on each element.
三, 22
是一个幻数(因为它既不 0
也不 1
)。这不应该是硬codeD。如果数字真的是一个编译时间常数,地方的名字。
Third, 22
is a "magic number" (because it's neither 0
nor 1
). This should not be hard-coded. If the number really is a compile-time constant, name it somewhere.
第四,无论哪种方式,不使用原始数组。无论是使用的std ::阵列
如果大小在编译时已知或的std ::矢量
如果的大小是在运行时确定的。
Fourth, either way, don't use raw arrays. Either use a std::array
if the size is known at compile-time, or a std::vector
if the size is determined at runtime.
第五,把他们放在一起,智能指针的动态容器照顾您所有的后顾之忧。这是在我的code psented一个$ P $。另一种选择,智能指针的静态数组,不会使用一个初始化函数所有,而是要正确当场进行初始化:
Fifth, putting it all together, a dynamic container of smart pointers takes care of all your worries. That's the one presented in my code. The alternative, a static array of smart pointers, wouldn't use an initialization function at all, but rather it'd be initialized right on the spot:
const std::size_t nfuncs = 22;
std::array<square_ptr, nfuncs> allSquares {
new PropertySquare1(1,"purple",120),
new PropertySquare1(2,"green",370),
// ...
};
这篇关于使用指针数组作为参数传递给方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!