0

This is what i have, but i am not sure that this works. I am not sure if copying this way is efficient,and i couldn't figure a way to overload. when copying, i am giving both the same size, setting header and tail(indexes of first and last element) to the same place,and simply copying every element of the array into the other. overloading seems tricky here. i have done overloading before but never with circular array queues.

#include <iostream>
using namespace std;

class CircArrayQueue
{
private:
    int *arr;
    int head;
    int tail;
    int size;
    CircArrayQueue()
    {
        arr = new int[5];
        head = -1;
        tail = -1;
    }
    CircArrayQueue(int s)
    {
        arr = new int[s];
        head = -1;
        tail = -1;
        size = s;
    }
    void copyQueue(CircArrayQueue &object)
    {   
        object.size = this->size;
        for (int i = 0; i < this->size; i++)
        {
            object.arr[i] = this->arr[i];
        }
        object.head = this->head;
        object.tail = this->tail;
    }
    ~CircArrayQueue()
    {
        delete[] arr;
    }


};
  • Those two look fine. – Ender_The_Xenocide Apr 25 '20 at 20:29
  • Probem I see is that there's no guarantee that `object.arr` and `this->arr` are the same size. But maybe that's guaranteed elsewhere in the code. – john Apr 25 '20 at 20:31
  • it isn't guaranteed, but i am trying to set them equal, do you have any idea about the assignment operator – Nadeem Zeaiter Apr 25 '20 at 20:33
  • @NadeemZeaiter Assignment opreator is best implemented by using [copy and swap.](https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom) – john Apr 25 '20 at 20:35
  • @NadeemZeaiter Really if you want to learn how to implement these operations you should read a book, it's too big a topic to explain in an internet post. This is a site for questions and answers not tutorials. – john Apr 25 '20 at 20:36
  • 1
    What are `head` and `tail`? And just setting `object.size` won't change the actual size of `object.arr`. Actually from this code it's not even clear if `object.arr` is a valid pointer at this point. – Lukas-T Apr 25 '20 at 20:36
  • i am using size as the actual size of the array in my implementation before, so size does actually control the size of the array, and i have a constructor, so when defining the object, arr is a dynamic array created of size as its size. head is the index of the first element, while tail is the index of the last one. – Nadeem Zeaiter Apr 25 '20 at 20:45
  • 1
    @NadeemZeaiter The point churill is making is that in `copyQueue` you change the value of `objject.size` without changing the size of `object.arr` which is at the very least dubious coding, but more than likely is a bug. Although without seeing the rest of the code it's impossible to be sure. – john Apr 25 '20 at 20:59
  • I'm not asking about a bug, since the code isn't done yet, however I've seen several ways for implementing copy constructors,and not enough clarifications, so i thought i'd ask, thanks anw – Nadeem Zeaiter Apr 25 '20 at 21:12
  • @john actually the copy swap idiom does answer my question – Nadeem Zeaiter Apr 25 '20 at 22:39
  • 1
    That's good. As a final tip: Unless you really want to take the hard way `std::vector` can make your life so much easier. – Lukas-T Apr 26 '20 at 07:06

1 Answers1

0

the perfect solution as I would get to learn is the copy-swap idiom, it does the job. What is the copy-and-swap idiom?