STL vector of vector scope and lifetime
I have a vector < vector < int > > myVec as a private member of a class.
This is allocated by a method setMyVec of a friend class. The problem I am
facing is that the inner vector int values are invalid when accessed from
any other method. I believe it is because the inner vectors are allocated
by setMyVec and deallocated when the method returns. Is that correct? If
so, how do I get around this problem? Here's some minimal example code.
class A {
vector< vector<int> > myVec;
public:
printMyVec();
friend class B;
};
class B {
public:
setMyVec();
};
B::setMyVec() {
int n = 100;
int m = 100;
int someNumber = 1000;
myVec.resize( n );
for(int i = 0; i < n; i++) {
myVec[i].resize( m );
for(int j = 0; j < m; j++) {
myVec[i][j] = someNumber;
}
}
}
No comments:
Post a Comment