Sign in to follow this  
ankhd

c++ STD::vectormyclass problem.

Recommended Posts

ankhd    2304
Hi all. I have a class say for now it's called myclass. It has the = operater overloaded. I went to use it like so Std::vector<myclass> data; for( int I = 0; I < newdatasize; I++) { data.push_back(newdata); data[I].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.

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by ankhd
The 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).

Share this post


Link to post
Share on other sites
ankhd    2304
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.

Share this post


Link to post
Share on other sites
ankhd    2304
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 just
Use push_back.

Share this post


Link to post
Share on other sites
jyk    2094
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 just
Use 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.

Share this post


Link to post
Share on other sites
ZMaster    240
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...

Share this post


Link to post
Share on other sites
ankhd    2304
Here is the whole thing how it was before I added the copy constructor

class 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 class

Used like this

Storeddata d;// allready loaded this data has Text set put I don't want to copy it over so I don't
Setit in = opperator

For(int I = 0; I < dsize; I++)
{
Myclass t;
t.Col = d[I].Col;
t.Emiss = d[I].Emiss;
t.Spec = d[I].Spec;
t.Power = d[I].Power;

myclass.push_back(t);// crashed out here before cpyconstuctor
myclass[I].LoadMaterial(fname[I]);
}

[Edited by - ankhd on May 3, 2009 4:43:16 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this