I am working with the following class that has a member to construct a list which can be subclassed with a template.
class dynamic_list
{
template <typename = void>
struct node
{
std::unique_ptr<node<>> next;
virtual ~node() = default;
virtual std::unique_ptr<node<>> clone() const = 0;
};
template <typename T>
struct typed_node : node<>
{
T value;
constexpr typed_node(const typed_node<T>& node)
: value(node.value)
{
}
template <typename Arg>
constexpr typed_node(const Arg& arg)
: value(arg)
{
}
std::unique_ptr<node<>> clone() const override
{
return std::unique_ptr<typed_node<T>>(new typed_node<T>(*this));
}
};
std::unique_ptr<node<>> head_;
public:
constexpr dynamic_list() = default;
template <typename T, typename Arg>
const T& push(const Arg& arg)
{
auto new_node = std::unique_ptr<typed_node<T>>(new typed_node<T>(arg));
auto& value = new_node->value;
new_node->next = std::move(head_);
head_ = std::move(new_node);
return value;
}
};
I am trying to write a copy constructor for this class and so far i have the following but it's not working:
dynamic_list(const dynamic_list& list)
{
if (list.head_)
{
head_ = list.head_->clone();
auto temp = head_.get();
auto list_temp = list.head_.get();
while (list_temp->next)
{
temp->next = list_temp->next->clone();
temp = temp->next.get();
list_temp = list_temp->next.get();
}
}
}
Any help would be much appreciated