   c++ STD::vectormyclass problem.
  Hi all. I have a class say for now it's called myclass. It has the = operater overloaded.  I went to use it like soStd::vector<myclass> data;for( int I = 0; I < newdatasize; I++){  data.push_back(newdata);  data.member = somevalue;}The problem is it will for some reason call the destructor of newdata. I can fix it by before using the vector calling resize and setting it to newdatasize,And it all works fine. My question is why does it not work when I don't first set the size of the list.
  Without seeing the code for "myclass", I would guess that your operator= contains a bug or that your copy constructor is at fault.   ;Quote:Original post by ankhdThe problem is it will for some reason call the destructor of newdata. It is supposed to do that. When the vector resizes itself, it has to put its elements into a new chunk of memory. The only way it can do that is to copy the elements into the new chunk of memory, and then destroy the originals (by calling destructors and then deallocating the old memory).Your responsibility is to make an assignment operator, copy constructor and destructor that do what's necessary to make this work properly. In some cases, this is as simple as doing nothing at all. In others, it's tricky enough that you'll want to follow established patterns, or use more sophisticated tools like smart pointers (which turn hard-to-copy data members into easy-to-copy ones, in turn letting you go back to the "do nothing at all" case, nearly all of the time).;  By copy constructor do you mean something like this. Class myclass{Public: Myclass(){}Myclass(myclass &mc)//is this the copy constructor{ //copy member from mc to this}If that's the copy constructor then no I don't have one. That then must be the problem thanks. I was thinking that I only needed the = operator. My bad. Thanks again I'll try it when I get home.   ;Quote:Original post by ankhdMyclass(myclass &mc)//is this the copy constructorNearly, a copy constructor looks like this:class myclass{public:    myclass(const myclass & mc); // Note the use of const};;  Thanks. It was indeed the copy constructors, as soon as I placed The constructor in the class and removed the resize call, and it worked. Now should I have it sized before the loop or it doesn't matter and justUse push_back.   ;Quote:It was indeed the copy constructors, as soon as I placed The constructor in the class and removed the resize call, and it worked.What worked, exactly? Were you getting a compiler error? A run-time error? More often than not, there's no need to implement any of the functions under discussion (destructor, = operator, and copy constructor) yourself, so I'm wondering what problem it was that you fixed by adding the copy constructor.Quote:Now should I have it sized before the loop or it doesn't matter and justUse push_back.It really only matters if you're worried about performance and/or how the memory is managed internally. For most purposes, you can just use push_back(). In some cases though, you may want to use reserve() or resize() to gain finer control over the underlying allocations/deallocations.   ;  Correct me if I'm wrong, but in your code snippet newdata appears to be some data on the stack. By pushing it onto your vector you are effectively producing a copy of the variable. Once newdata goes out of scope, the destructor is going to be called on it.I don't know why exactly you would want to prevent the destructor from being called, but one of my guesses would be that you are allocating some data on the heap in your constructor and that you are deallocating that data in the destructor. This might be dangerous if you don't override the copy constructor and are not using some sort of smart/scoped pointer or garbage collector. You might want to make your copy constructor produce a deep copy (copy the data on the heap, not just the pointer to the data) of your class - if that is the case...   ;  Here is the whole thing how it was before I added the copy constructorclass myclass {public:    myclass()    {  Col = 0;  Emiss = 0; Spec = 0; TextRV = 0; Power = 1;    }~myclass(){SAFE_RELEASE(TextRV);//here is where it breaks with out copy constructor } ID3D10ShaderResourceView *TextRV;// D3DX10VECTOR4 col; D3DX10VECTOR3  Spec; D3DX10VECTOR3. Emiss;   Float Power;};//end classUsed like thisStoreddata d;// allready loaded this data has Text set put I don't want to copy it over so I don'tSetit in = opperator For(int I = 0; I < dsize; I++){ Myclass t; t.Col = d.Col; t.Emiss = d.Emiss;t.Spec = d.Spec;t.Power = d.Power; myclass.push_back(t);// crashed out here before cpyconstuctor myclass.LoadMaterial(fname);}[Edited by - ankhd on May 3, 2009 4:43:16 AM]