I have a question for c++ novice const. "T"

Asked 2 years ago, Updated 2 years ago, 17 views

// Node structure
template <typename T>
struct Node {
    T idata;
    Node<T>* ptr_next;
    Node<T>* ptr_prev;

    // Initialization when no value is entered,
    Node() :

        idata(0),
        ptr_next(nullptr),
        ptr_prev(nullptr) {}

    // Initialize when a value is entered 
    Node (const T&_idata, const Node<T>* next, const Node<T>* prev) // @@Question part@@
        : : idata(_idata)
        , , ptr_next(next)
        , , ptr_prev(prev) {}

};                                  

The linked list is being created in the form of a class template. The node part is being structured.

As shown in the code above, the arguments of the constructor portion of the node are 2nd and 3rd

If I put const, there will be an error, so what is the reason?

There was no problem with the first const.

If I upload the whole code, it's too long. I thought you wouldn't read it, so I just uploaded the node part

c++

2022-09-20 11:18

2 Answers

If const is played before the argument, the argument is a constant. In other words, we will never change that value within this functionIt states that it is .

There is no problem with the code I just posted in the question.

This is probably an error caused by a code that substitutes values for next and prev in a code that was not posted because it was long in the question.

Variables with const can only be read. If there is a code that changes a variable with const, the compiler marks it as an error.


2022-09-20 11:18

template <typename T>
void CList<T>::push_back(const T& idata) {

    Node<T>* ptr_Node = new Node<T>(idata, nullptr, nullptr);

    if (0 == this->icount )
    {
        this->ptr_head = ptr_Node;
        this->ptr_tail = ptr_Node;
     } } else {
        ptr_Node->ptr_prev = this->ptr_tail;
        this->ptr_tail->ptr_next = ptr_Node;
        this->ptr_tail = ptr_Node;
     }
            ++this->icount;

}

Is there anything wrong here?
I don't know even though I've seen it many times. I will capture the error part and upload it.

Thank you very much for answering my ambiguous question


2022-09-20 11:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.