Sunday, 15 July 2012

c++ - A strange phenomenon of vector member in a class -



c++ - A strange phenomenon of vector member in a class -

all header file included,

class test{ public: vector<int> vector_int(10); test(int num); ~test(); }; test::test(int num){ cout << "constructor employed" <<endl; vector<int> vector_int; (int = 0; < num; i++){ vector_int.push_back(i); } cout<<"the size " << vector_int.size()<<endl; } int main(){ int num = 10; test* = new test(10); cout<< a->vector_int.size()<< " size"<<endl; homecoming 0; }

result: constructor employed size 10

0 size

it seems me these sizes should same. constructor employed. problem then?

you shadowing class fellow member variable vector_int local variable of same name in constructor.

remove line vector<int> vector_int; constructor , should work expected.

c++ vector

No comments:

Post a Comment