This is the code to reproduce what I'm trying to understand:
First I have a class with a pointer as attribute and its contructor set to default.
class PointExampl {
private:
int* p_ = new int;
public:
PointExampl() = default;
int* returnp() {return p_;};
};
This is the main.cpp:
int main() {
std::vector<std::vector<PointExampl>> vec;
vec.resize(10, std::vector<PointExampl>(10));
for(int i = 0; i < vec.size(); i++)
std::cout << vec[i][0].returnp() << std::endl;
return 0;
}
And this is the output:
0x1695c8e1920
0x1695c8e1920
0x1695c8e1920
0x1695c8e1920
0x1695c8e1920
0x1695c8e1920
0x1695c8e1920
0x1695c8e1920
0x1695c8e1920
0x1695c8e1920
So this is what i dont get about std::resize . Shouldn't every first object of every row have a different pointer?
This is simplification of a problem that i got using std::vector for some class project about game of life. I was expecting to have a full matrix of new pointers but instead got a copy of the first row pointers to the other rows.
also im using c++14 if that has anything to do about it.