Returns a pointer to member variable?

  c++

I tried to implement encapsulation for my linked list node class, but it seems that I did something wrong. How can I make a function return a pointer to a member variable inside class?

template <typename T>
class DoublyLinkListNode
{
public:
    using value_type = T;
    using reference = value_type&;
    using const_reference = const value_type&;
    using size_type = size_t;

    DoublyLinkListNode() :m_value(value_type()), m_prev(nullptr), m_next(nullptr) {}
    DoublyLinkListNode(const_reference p_value) : m_value(p_value), m_prev(nullptr), m_next(nullptr) {}
    DoublyLinkListNode(const_reference p_value, DoublyLinkListNode* p_prev, DoublyLinkListNode* p_next) : m_value(p_value), m_prev(p_prev), m_next(p_next) {}
    DoublyLinkListNode(value_type&& p_value, DoublyLinkListNode* p_prev, DoublyLinkListNode* p_next) : m_value(p_value), m_prev(p_prev), m_next(p_next) { p_value = value_type(); }

    // Dereference that allows us to write/read into Elem.
    reference operator*() { return m_value; }
    const_reference operator*() const { return m_value; }

    DoublyLinkListNode* previous() { return m_prev; }
    //const DoublyLinkListNode* previous() const { return m_prev; }
    DoublyLinkListNode* next() { return m_next; }
    //const DoublyLinkListNode* next() const { return m_next; }

    DoublyLinkListNode* m_prev; // previous link.
    DoublyLinkListNode* m_next; // succesor (next) link.
    T m_value;
};
int main()
{
    DoublyLinkListNode<double>* node = new DoublyLinkListNode(5.5);
    DoublyLinkListNode<double> node2(5.5);
    DoublyLinkListNode<double>* node3 = new DoublyLinkListNode(5.5);

    // expression must be a modifiable lvalue?
    node->previous() = node3;

    std::cout << node->operator*() << std::endl;
    return 0;
}

Source: Windows Questions C++

LEAVE A COMMENT