OK, you guys were very helpful with my last question, so I will try another one. It is also homework and when the last one was very old, it has been presented and is waiting to be marked. So if I cut something off then this would probably be a problem. I have obscured the names of class and that is because it is still possible to submit the assignment (for other students).
I have a class whose only member is an indicator for the object, this class is currently designed to highlight some operations from the holding pointer - object * o_ J Object {1, 2, 3, ...} . Now, I'm able to do any of the following without leaking or crashing the memory.
std :: vector & lt; ObjectPtr & gt; V; V.push_back (object PTR (new object 1 (..., ..., ...))); V.push_back (object PTR (new object 2 (..., ...)); V.push_back (objectPrint (new object1 (.., .., ..))); // Copy Constructor PTR std :: vector & lt; ObjectPtr & gt; V2 (v); // Assignment operator PTR std :: vector & lt; ObjectPtr & gt; V3; V3 = v2; All this works, and there is no memory leak. But if I use istream_iterator & lt; ObjectPtr & gt; I try to read the contents in the file with then it starts leaking ObjectPtr is the only class handling dynamic memory and the object * o_ is set to either the tap or < Code> object {1, 2, 3, ...} .
The file looks like this
< Code> std :: ifstream ("file.txt"); Std :: istream_iterator & lt; ObjectPtr & gt; In (is), end; For (; in! = End; ++ in) cout & lt; & Lt; * In the & lt; & Lt; "\ N"; The friends function in ObjectPtr used for reading in these values
friend isterm and operator> & Gt; Istream & amp; Is, objectpit and option) {std :: string tmp; While (std :: getline (tmp,)) {if (tmp == "Object1") {op.o_ = new Object1; Return is & gt; & Gt; (Object 1 end); (Op.o_); // Send it to operator & gt; & Gt; For object 1} if (TMP == "object 2") {op.o_ = new object 2; Return is & gt; & Gt; (Object 2 & amp;) * (op.o_); } ...} is a return; } Somewhere it becomes unresponsive to me, and I really want to know why in short - istream_iterator leak memory while assignment and copy constructor works properly, Which I believe is that the classes the object {1, 2, 3, 4, ..} has been created correctly, and the problem is operator & gt; & Gt; .
Here is the first thing for me that I do not know whether this is the issue for which you are hunting:
Friends iron and operator> gt; & Gt; (Istream & amp; Object PTR & amp; op) {std :: string tmp; While (std :: getline (tmp,)) {if (tmp == "Object1") {op.o_ = new Object1; In that last line, what is the old value in op.o ?
Remember, streaming in any object means the thing completely created and you have to keep in mind the old data of the object. (This is the reason why often like to take the constructor to std :: istream for complex objects, which secures the initialization for an object that is changed in the next moment. Can be.)
whether ObjectPtr is an assignment operator or swap () member functions? If so, by implementing a new object the input operator can be easy to implement and can swap / swap it from op
Comments
Post a Comment