Public Group

# c++ STD::vectormyclass problem.

This topic is 3487 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.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 on other sites
Without seeing the code for "myclass", I would guess that your operator= contains a bug or that your copy constructor is at fault.

##### Share on other sites
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).

##### Share on other sites
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 on other sites
Quote:
 Original post by ankhdMyclass(myclass &mc)//is this the copy constructor
Nearly, a copy constructor looks like this:

class myclass{public:    myclass(const myclass & mc); // Note the use of const};

##### Share on other sites
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 on other sites
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.

##### Share on other sites
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 on other sites
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
}

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.Col;
t.Emiss = d.Emiss;
t.Spec = d.Spec;
t.Power = d.Power;

myclass.push_back(t);// crashed out here before cpyconstuctor
}

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

1. 1
Rutin
42
2. 2
3. 3
4. 4
5. 5

• 18
• 20
• 14
• 14
• 9
• ### Forum Statistics

• Total Topics
633375
• Total Posts
3011554
• ### Who's Online (See full list)

There are no registered users currently online

×