1
\$\begingroup\$

This is my first code using templates and I wanted to know if the code looks good?

I have some doubts like what's the best way to report error to user (queue full/empty), I checked a link online http://www.cprogramming.com/tutorial/computersciencetheory/queuecode.html where they use assert() so I decided to use it in my code. Also how to let user know to catch out of memory exception when using the constructor for Queue class.

EDIT: Removed assertions.

#include <iostream>
using namespace std;
template <class T>
class Queue{
private:
    T * data;
    size_t cap;
    int cnt;
    int head, tail;

public:

    Queue(size_t c){
        cap = c;
        data = new T[c];
        head = -1;
        tail = -1;
        cnt = 0;
    }

    ~Queue(){
        delete []data;
    }
    //A return value of -1 indicates failure, and 1 indicates success
    int enqueue(T val){

        if(cnt == cap)
            return -1;
        if(head==-1)
            head++;

        tail = (tail+1)%cap;
        data[tail] = val;
        cnt++;
        return 1;

    }
    //A return value of -1 indicates failure, and 1 indicates success
    int dequeue(T *res){

        if(!res || cnt==0)
            return -1;

        *res = data[head];
        head = (head+1)%cap;
        cnt--;

        return 1;

    }
    //A return value of -1 indicates failure, and 1 indicates success
    int front(T *res){

        if(cnt==0 || !res)
            return -1;

        *res = data[head];
        return 1;
    }


};
\$\endgroup\$
  • \$\begingroup\$ assert() will vanish away on release builds, along with any invariance the code had. That actually forced me to revert a few commits in my repo. Also, the style is very C-ish, I recommend trying to use standard library first, then trying to reimplement it. \$\endgroup\$ – Incomputable Mar 27 '17 at 0:14
  • \$\begingroup\$ I removed the assertions. What else is C-ish? \$\endgroup\$ – zorro Mar 27 '17 at 0:28
  • \$\begingroup\$ as you probably know, our site is not for hotline code improvements :) I recommend to get some reviews, and they will probably mention all of those. You could also read some C++ repositories or questions from highly recognized users, such as Loki, Jerry, Emily, etc. (my favorites). There is a very big difference in how code looks, in lots of ways. If you're interested in template madness, then my questions are the best fit :) Also, your post description doesn't correspond to your code anymore. \$\endgroup\$ – Incomputable Mar 27 '17 at 0:31
  • \$\begingroup\$ @incomputable Asserts are completely okay if used as asserts. Alot of companies ask you to use them as much as you can (for checking pre and postconditions) \$\endgroup\$ – Maikel Mar 27 '17 at 6:44
  • \$\begingroup\$ @Maikel, I might have incorrectly worded it. I'm not saying do not check for anything, I'm saying do not use assert for that. If one doesn't have a build script that will make the right build and run unit tests against it, one can accidentally build release build and expect that tests passed. This is what happened to me a week ago. \$\endgroup\$ – Incomputable Mar 27 '17 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.