Copy Constructor - Problem with making exact copy

In the class:
value_type *data;
size_type used;
size_type capacity;
size_type current_index;


sequence::sequence(const sequence& entry)
data = new value_type[entry.capacity];
/* copy constructor ensures that the new object will have its own dynamically allocated memory */

used = entry.used;
current_index = entry.current_index;

}/* end of copy construct

The program is buggy and when I run it it give me garbage when prompted to display. I've to make copies since it's a copy constructor; however, I know I'm missing something, but can't figure it out.
I might be over looking it or may be not looking at it all.

Could you please assist me what is going on, and what needs to fixed. Thanks!
Last edited on
You are allocating the buffer properly... but are you actually copying the data over? You aren't in the code you posted there.
Yes, the actual data present in the entry object is not been copied to this->data
This is what i added to it, but it's not running..

I'm copying over the data with the for loop.

for ( int i = 0; i > 0 ; i < used)[i] =[i];

used = entry.used;
That for loop is ill formed and will not run at all.

It also would not even compile, because this is a pointer and not an object, therefore you can't use the dot operator with it.

If there's a bug in the code, we're not going to see it if you don't post the actual bugged code. If you retype the code that doesn't tell us anything because you may have fixed/introduced bugs in the retype.
Last edited on
For your loop you dont need the i>0 you already set the inital value to 0 in the i=0 you're better off with
for(int i=0;i<used;i++){
Last edited on
Topic archived. No new replies allowed.