正确初始化基类构造函数中的unique_ptr
Initializing a unique_ptr in constructor of base class properly
我尝试将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;
}
}
相关文章:
- 为什么 std::unique 不调用 std::sort?
- CLANG 编译器 说:变量"PTR"可能未初始化
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁
- 将 ptr 传递给 ptr 到 A 作为参数传递给 A 的函数是不好的做法吗?
- 为共享 ptr 向量实现复制 c'tor?
- 字符和整数中 **(ptr+1) 的值差异
- C++:在不中断共享的情况下通过引用传递共享 PTR?
- 生成"unique"矩阵
- 如何将派生类从基 ptr 分配给 nlohmann::json
- 引用 std::shared:ptr 以避免引用计数
- 我对 std::unique(算法)C++有问题
- 为什么我不能在不进行任何转换的情况下将浮点数放入任何类型的 ptr 中?
- 在调用函数时,ptr** 和 ptr*& 之间是否有区别,或者首选C++?
- 另一种类型的智能ptr,比如具有弱refs的unique_ptr
- 尝试打印出 *ptr++ 的值,以了解它是如何工作的
- 如何控制共享 ptr 引用计数?
- std::shared_ptr::unique(),复制和线程安全
- 如何在C++03中用自定义谓词调用std::unique
- C++中的指针否定 (!ptr == NULL)
- C++14 unique_ptr并使用已删除的函数'std::unique-ptr' unique_ptr错误