正确初始化基类构造函数中的unique_ptr

Initializing a unique_ptr in constructor of base class properly

本文关键字:unique ptr 构造函数 初始化 基类      更新时间:2023-10-16

我尝试将std::unique_ptr传递给继承类,这将把它转发给基类构造函数(使用构造函数初始化列表)。如果基类构造函数接收到nullptr,则应该构造一个默认对象并将其赋值给基类中的std::unique_ptr成员变量。但不知何故,我得到一个AccessViolation,如果我试图访问任何地方的std::unique_ptr的任何元素(因为它在某种程度上仍然是一个nullptr -即使这应该是不可能的,在这个时候)。

你知道这里出了什么问题吗?

#include <iostream>
#include <memory>
class C{
public:
  int x;
};
class A{
public:
  A(std::unique_ptr<C> c) : c(std::move(c)){
    if(c == nullptr){
      c = std::unique_ptr<C>(new C);
      c->x = 1;
    }
  }
  void print(){
    std::cout << c->x << std::endl;
  }
private:
  std::unique_ptr<C> c;
};
class B : public A{
public:
  B(std::unique_ptr<C> c) : A(std::move(c)){
  }
};
int main(int argc, char* argv[]){
  B b(nullptr);
  b.print();
  return 0;
}
https://ideone.com/fHvYqe

A::ctor中,您正在使用变量c,但它不是引用类成员A::c,而是引用局部变量c,这是ctor参数。所以在ctor退出后,A::c将是nullptr,所以你不能在A::print函数中解引用它。

  A(std::unique_ptr<C> c) : c(std::move(c)){
    if(c == nullptr) {               // here c is ctor parameter (local variable)
      c = std::unique_ptr<C>(new C); // A:c is still nullptr
      c->x = 1;                      // 
    }
  }

可能的解决方案是为局部变量c name和A::c取不同的名字,例如A::m_c

如果您不想在构造函数中做任何额外的工作,并且提供基类的所有可能的构造函数不是问题,请使用继承构造函数。

class B : public A {
  using A::A;
};

结果是变量的命名非常糟糕。

构造函数的形参与成员变量同名。因此,只有构造函数的变量在构造函数体中创建,成员变量在初始化列表中赋值,无论你传递什么(在你的例子中是nullptr)。

要解决这个问题,重命名构造函数的参数:

  A(std::unique_ptr<C> c1) : c(std::move(c1)){
    if(c == nullptr){
      c = std::unique_ptr<C>(new C);
      c->x = 1;
    }
  }