I am making a very dumb mistake, just wrapping an indicator into some ordinary memory in a new class. Class matrix {public: matrix (int, int h): width (w), height (h) {data = new unsigned four [width * height]; } ~ Matrix () {delete data; } Matrix & amp; Matrix :: operator = (Const Metrix and P) {width = p.width; Height = p. height; Data = p.data; Return * This; } Width Height; Unsigned char * data; } ......... // main code std :: vector & lt; Matrix & gt; some data; For (int i = 0; i & lt; n; i ++) {some_data.push_back (matrix (100,100)); // All Matrix Data Points are the same}}
When I fill the vector with the examples of the class, then the internal data signals all point to the same memory?
1 You are missing the copy constructor.
2 Your assignment operator should not just copy the pointer because it leaves multiple matrix objects from the same data pointer, whose Meaning that the pointer will delete d several times. Instead, you should make a deep copy of the matrix. See how @Geiman provides complete information about how to write an efficient, exception-protected operator = function.
3 You delete [] in your Destructor, Delete No.
Comments
Post a Comment