我是新的C++(从Java来)和我实际上与 struggeling以下: 让FOO是类C++创建的对象与一个数组作为构件
int bar[10] = {};
Foo::Foo()
{
bar[1] = 42;
}
和doSmth()在主类中的方法:
Foo doSmth(){
Foo f;
f.bar[0] = 10;
return f;
}
主要会是这样的:
int main(int argc, char *argv[])
{
Foo f = doSmth();
cout << f.bar[1] << endl;
cout << f.bar[0] << endl;
return 0;
}
这是否正确地返回foo
- 对象doSmth()
? 我想在堆栈上创建对象,但我担心foo对象中的array (bar)
,它何时会从堆栈中删除?
为什么没有注释的downvote? – zipzit