Copy Constructor help

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];
}
What would you expect to happen?

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]
Your not really showing enough content. I suggest you show the smallest complete program that illustrates the problem.


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.