Search code examples
c++pointerslvaluemove-constructor

How to get pointer to new variable when using a move constructor


So I've got this code:

//movable_ptr.hpp
//Michal Cermak

template<typename T> class movable_ptr;

template<typename T> class enable_movable_ptr {
public:
    //default constructor
    enable_movable_ptr() {};

    //move constructor and assignment
    enable_movable_ptr(enable_movable_ptr<T>&& p) {
        first_ = p.getFirst();
        p.retarget_to(this);
    };
    enable_movable_ptr<T>& operator=(enable_movable_ptr<T>&& p) {
        if (this != &p)
        {
            first_ = p.getFirst();
            p.retarget_to(this);
            delete &p;
        }
        return *this;
    };

    //retargets all pointers in the linked list to a new address
    void retarget_to(T* p)
    {
        if (first_ != nullptr)
        {
            auto current = first_;
            do
            {
                current->set(p);
                current = current->getNext();
            } while (current != first_);
        }
    };

    movable_ptr<T>* getFirst() { return first_; };
    void setFirst(movable_ptr<T>* p) { first_ = p; };
private:
    movable_ptr<T>* first_ = nullptr;
};

template<typename T> class movable_ptr {
public:
    //constructors and stuff...

    //access to variables
    T* get() {return ptr_; };
    void set(T* p) { ptr_ = p; };
    movable_ptr<T>* getNext() { return next_; };
    void setNext(movable_ptr<T>* p) { next_ = p; };
    movable_ptr<T>* getPrevious() {return prev_; };
    void setPrevious(movable_ptr<T>* p) { prev_ = p; };

private:
    T* ptr_ = nullptr;
    movable_ptr<T>* next_ = this;
    movable_ptr<T>* prev_ = this;
};

My problem is that I need to give T * to retarget_to, but I use retarget_to(this) in the move constructor and assignment in enable_movable_ptr. That passes it enable_movable_ptr<T> * instead of just T *. The thing is, I assume that T inherits from enable_movable_ptr, which will never be used directly, only through the object that inherits from it. For example:

class A : public enable_movable_ptr<A>
{
public:
    int val;

    A(int val) : val(val) {}
};

And then used like this:

A x(42);
A y = move(x);

In this case, this would be enable_movable_ptr<A> *, but I need something that would give me A * instead. Basically I need a pointer to the lvalue of the = operator, while inside an overload of said operator. Is there any way to do this or am I asking for something impossible?


Solution

  • I haven't understood your question entirely, because it's not clear what you want to achieve with this enable_movable_ptr class. I think your way of writing operator= is ill-formed. You are trying to explicitly call delete on a pointer to r-value (which may firstly be allocated on stack, and moreover probably will be destroyed later anyway via some other mechanism).

    I would suggest to consider copy-and-swap approach for operator=, this would allow you to not worry about checking if you are assigning object into itself. The signature would be enable_movable_ptr<T>& operator=(enable_movable_ptr<T> other) (note passing by value).