相关疑难解决方法(0)

std :: unique_ptr <>作为基于节点的结构中的指针

由于大多数人都喜欢谜题,我会用一个(拼写错误:)开始这个问题.想得到介绍,请注意,如果你不关心它,你可以跳过热身(JG问题)并阅读G问题,因为那是我的"真正的问题".

在审查潜在新员工提供的代码示例时,您偶然发现了一个链接列表,其实现使用了现代C++ 11特性,即std :: unique_ptr <>.

template <typename T> 
struct Node { 
   T data; 
   std::unique_ptr<Node<T>> next; 
   Node () {} 
   Node(const T& data_): data(data_) {} 
   Node(Node& other) { std::static_assert(false,"OH NOES"); } 
   Node& operator= (const Node& other) { 
     std::static_assert(false,"OH NOES"); 
     return *new Node(); 
   } 
public: 
   void addNext(const T& t) { 
      next.reset(new Node<T>(t)); 
   }
};

template<typename T>
class FwdList
{
    std::unique_ptr<Node<T>> head;
public:
    void add(const T& t)
    {
        if (head == nullptr)
            head.reset( new Node<T>(t));
        else {
            Node<T>* curr_node = head.get(); …
Run Code Online (Sandbox Code Playgroud)

c++ unique-ptr c++11

5
推荐指数
1
解决办法
1984
查看次数

标签 统计

c++ ×1

c++11 ×1

unique-ptr ×1