As the title shows, I find it hard to come up for a good description of the problem I encountered.
Please can some of you guru's help me?
This is a distilled segment of the code. The actual code is much bigger.Code:#include <vector> class sD { private: float* _d; public: sD(){}; ~sD(){ delete _d; } sD(const sD & D):_d(new float((*D._d))){} sD(float d){ _d = new float(d); } operator float(){ return *_d; }; class tst{ private: std::vector<sD> vec; public: tst(){}; void insert(float sD){ vec.insert(vec.begin(), sD); } float operator[](std::vector<sD>::size_type i){ return vec[i]; } }; int main(){ tst t; t.insert(10); t.insert(20); float tmp1 = t[1];// tmp1 holds 10 float tmp2 = t[0];// tmp2 holds ? }
I would expect tmp2 to be 20.
If I replace "vec.insert(vec.begin(), sD);" with "vec.push_back(sD);" than as expected; tmp2 holds 10 and tmp1 holds 20.
Why doesn’t the tmp2 of my code hold 20? How can I remedy this behaviour?




Reply With Quote
