Copy Constructor help

Oct 6, 2018 at 10:27pm
Hi I'm writing a copy constructor for a queue class but for some reason this code makes both the front and capacity of the copy the same value. This doesn't make any sense to me. Am I missing something?

1
2
3
4
5
6
7
  queue::queue(const queue& old_queue ){
    _size = old_queue._size;
    _front = old_queue._front;
    _capacity = old_queue._capacity;
    for(int i = 0; i < _capacity; i++ )
      _arr[i] = old_queue._arr[i];
}
Oct 6, 2018 at 11:07pm
What would you expect to happen?

Oct 7, 2018 at 12:49am
Here we would expect q1.capacity() = q2.capacity() = 8 and q1.front() = q2.front() = 1
But for some reason I'm getting q1.capacity() = 8 q1.front() = 1 q2.capacity() = 1 q2.front() = 1

[\code]
queue q1;
q1.enqueue(1);
q1.enqueue(2);
q1.enqueue(3);
q1.enqueue(4);
q1.enqueue(5);
q1.enqueue(6);
q1.enqueue(7);
assert(q1.size()==7);
assert(q1.capacity()==8);
assert(q1.front()==1);

// copy constructor
queue q2 = q1;
assert(q2.size()==7);

assert(q2.capacity()==8);
assert(q2.front()==1);
[\code]
Oct 7, 2018 at 2:44am
Your not really showing enough content. I suggest you show the smallest complete program that illustrates the problem.


Oct 7, 2018 at 12:15pm
Try to comment out your defined copy constructor. Then see if you get the results you want from the default copy constructor.
Topic archived. No new replies allowed.