【发布时间】:2009-08-03 01:41:56
【问题描述】:
假设我有以下代码。
double *return_array(void) {
double foo[2];
foo[0] = 5;
foo[1] = 6;
cout << foo << endl;
cout << foo[0] << endl << foo[1] << endl;
return foo;
}
double *bar = return_array()
cout << bar << endl;
cout << bar[0] << endl << bar[1] << endl;
现在,bar 和 foo 仍然是同一个指针,但那里的内容已经完全改变了。我怎样才能解决这个问题?基本上,我需要从一个函数中传递 6 个或 9 个双打。我该怎么办?
【问题讨论】:
-
除了下面的好回复之外,如果您有固定数量的值,您可能希望用它们创建一个 struct|class 并返回它。根据您的值的使用,它可能更容易阅读/处理,例如使用 g=foo[gravity] 代替: g=foo.gravity