vectors with custom class
| afraidofdark (4) | |||||
| hello I am trying to create a vector that holds qwe class, In qwe class, constructor allocate places for x, y and z variables. In deconstructor these allocated memories are deleted. However when I push a new instance of qwe in my vector. 1-It calls deconstructor many times and that couse delete x,y,z variables more than once, by the way I dont know why deconstructor is called ? 2- what should I do to learn if a pointer is deleted before is this correct ?
and there is the code that I worked on
My english may include mistakes, soryy for them and thanks for your help | |||||
| DiptenduDas (84) | |||
| U need to overload the copy constructor for class qwe as it contains the pointers to double which are created in constructor. So the same memory is deleted twice, which is causing the de-allocation. | |||
| afraidofdark (4) | |||
with this change in the class
qwe(const qwe &cSource);
its working without any error thanx for your help | |||
| jlamothe (14) | |||
| Vectors frequently do funny things, like copying objects and deleting the originals. When you have a class that allocates memory for itself, it's always a good idea to overload the = operator as well as creating a constructor that copies another object of the same class. I recommend adding the following constructor and function:
| |||
| jsmith (706) | |||
| One other thing in regards to the original post.... new will throw a bad_alloc exception if there isn't sufficient memory to allocate; therefore, unless you are using the nothrow version of new (which you're not by default), the correct way to check for new failure is as follows:
| |||
This topic is archived - New replies not allowed.
