Skip to main content
Commonmark migration
Source Link

Issues in your current code.

##Issues in your current code.
YouYou don't implement the rule of three/five.

##Issues in your current code.
You don't implement the rule of three/five.

Issues in your current code.

You don't implement the rule of three/five.

added 254 characters in body
Source Link
Loki Astari
  • 96.6k
  • 5
  • 125
  • 338
void push_back(const T &_data)
{
    new Node(_data, sent, sent->prev);
    ++size_;
}

void push_front(const T &_data)
{
    new Node(_data, sent->next, sent);
    ++size_;
}

Node::Node(T const& data, Node* next, Node* prev)
    : data(data)
    , next(next)
    , prev(prev)
{
    prev->next = this;
    next->prev = this;
}

void pop_back()
{
    if (size > 0) {
        delete sent->prev;
        --size_;
    }
}
void pop_front()
{
    if (size > 0) {
        delete sent->next;
        --size_;
    }
}

Node::~Node()
{
    prev->next = next;
    next->prev = prev;
}

Next you always copy the objects into the list. That's fine but you should also think about moving objects into the list and emplacing them (building them in place).

void push_back(T const& copy);     // You have this.
void push_back(T&& move);          // Move into the list
template<typename... Args>
void emplace_back(Args...&& args); // Build in-place

You don't need two version of size.

// This version is not necessary.
// The const version will work in all situations.
size_t size() {     
    return size_;
}

size_t size() const {
    return size_;
}

Youre const methods for getting iterators does not return the correct type.

Iterator begin() const {
    return Iterator(head, tail);
} 

Iterator end() const {
    Iterator iter(nullptr, tail);
    return iter;
}

Technically you can still use the iterators returned to modify the object. So these should not really callable from a const version of the object. From the const version of begin() and end you should return a const version of the iterator that allows you to read the container but not write to it.

While we are on iterators you should also add cbegin() and cend() that always return const iterators.

void push_back(const T &_data)
{
    new Node(_data, sent, sent->prev);
    ++size_;
}

void push_front(const T &_data)
{
    new Node(_data, sent->next, sent);
    ++size_;
}

void pop_back()
{
    if (size > 0) {
        delete sent->prev;
        --size_;
    }
}
void pop_front()
{
    if (size > 0) {
        delete sent->next;
        --size_;
    }
}
void push_back(const T &_data)
{
    new Node(_data, sent, sent->prev);
    ++size_;
}

void push_front(const T &_data)
{
    new Node(_data, sent->next, sent);
    ++size_;
}

Node::Node(T const& data, Node* next, Node* prev)
    : data(data)
    , next(next)
    , prev(prev)
{
    prev->next = this;
    next->prev = this;
}

void pop_back()
{
    if (size > 0) {
        delete sent->prev;
        --size_;
    }
}
void pop_front()
{
    if (size > 0) {
        delete sent->next;
        --size_;
    }
}

Node::~Node()
{
    prev->next = next;
    next->prev = prev;
}

Next you always copy the objects into the list. That's fine but you should also think about moving objects into the list and emplacing them (building them in place).

void push_back(T const& copy);     // You have this.
void push_back(T&& move);          // Move into the list
template<typename... Args>
void emplace_back(Args...&& args); // Build in-place

You don't need two version of size.

// This version is not necessary.
// The const version will work in all situations.
size_t size() {     
    return size_;
}

size_t size() const {
    return size_;
}

Youre const methods for getting iterators does not return the correct type.

Iterator begin() const {
    return Iterator(head, tail);
} 

Iterator end() const {
    Iterator iter(nullptr, tail);
    return iter;
}

Technically you can still use the iterators returned to modify the object. So these should not really callable from a const version of the object. From the const version of begin() and end you should return a const version of the iterator that allows you to read the container but not write to it.

While we are on iterators you should also add cbegin() and cend() that always return const iterators.

added 254 characters in body
Source Link
Loki Astari
  • 96.6k
  • 5
  • 125
  • 338
void push_back(const T &_data)
{
    new Node(_data, sent, sent->prev);
    ++size_;
}

void push_front(const T &_data)  
{
    new Node(_data, sent->next, sent);
    ++size_;
}

void pop_back()
{
    if (size > 0) {
        delete sent->prev;
        --size_;
    }
}
void pop_front()
{
    if (size > 0) {
        delete sent->next;
        --size_;
    }
}
void push_back(const T &_data)
{
    new Node(_data, sent, sent->prev);
    ++size_;
}

void push_front(const T &_data) {
    new Node(_data, sent->next, sent);
    ++size_;
}
void push_back(const T &_data)
{
    new Node(_data, sent, sent->prev);
    ++size_;
}

void push_front(const T &_data) 
{
    new Node(_data, sent->next, sent);
    ++size_;
}

void pop_back()
{
    if (size > 0) {
        delete sent->prev;
        --size_;
    }
}
void pop_front()
{
    if (size > 0) {
        delete sent->next;
        --size_;
    }
}
Source Link
Loki Astari
  • 96.6k
  • 5
  • 125
  • 338
Loading