w*****3 发帖数: 101 | 1 class Foo{
public:
Foo(int capacity);
....
private:
vector _stack;
....
};//class Foo
Foo(int capacity){ //Foo1
//wrong, _stack still empty outside the constructor scope
vector_stack(capacity);
}
Foo(int capacity){ //Foo2
//right, _stack initialized
_stack = vector(capacity;
}
Questions:
why vector_stack(capacity); in the constructor Foo1 doesn't work
Thanks |
h****8 发帖数: 599 | 2 因为foo1里面你又新定义并且初始化了一个变量_stack,不是原来那个成员变量
把vector去掉,改为 _stack(capacity);就行了 |
z****e 发帖数: 2024 | 3 a new local vector is defined in your ctor. |
p***o 发帖数: 1252 | 4 That won't work. Search "initialization list" in google and read.
【在 h****8 的大作中提到】 : 因为foo1里面你又新定义并且初始化了一个变量_stack,不是原来那个成员变量 : 把vector去掉,改为 _stack(capacity);就行了
|
w*****3 发帖数: 101 | |
h****8 发帖数: 599 | 6 你说的对 应该写成_stack.reserve(capacity)
【在 p***o 的大作中提到】 : That won't work. Search "initialization list" in google and read.
|