C++ unique_ptr 从派生到基础的转换

标签 c++ c++17 unique-ptr

我一直在四处阅读,并且正在努力理解并让它发挥作用。

我有一个基类 Person,Teacher & Student 继承自它。我想将它们都存储在“人”类型的 vector 中。我尝试了一些东西。但是,我不断收到错误页面,而且我很难理解它们。

当前代码是用 g++ -std=c++17 test.cpp 编译的 给我:

Undefined symbols for architecture x86_64:
  "Person::~Person()", referenced from:
      Teacher::~Teacher() in test-9423bf.o
      Student::~Student() in test-9423bf.o
ld: symbol(s) not found for architecture x86_64

如果有任何关于简单编写的 C++ 功能的提示和良好引用,我们将不胜感激。

#include <iostream>
#include <vector>
#include <memory>

class Person {
public:
    virtual void printName() = 0;
    virtual ~Person() = 0;
};

class Teacher : public Person {
public:
    void printName() {
        std::cout << "Hello My Name is Teacher" << std::endl;
    }
    ~Teacher() {}

};

class Student : public Person {
public:
    void printName() {
        std::cout << "Hello My Name Is Student" << std::endl;
    }

    ~Student() {}

};

//Capturing the raw pointer and letting it go out of scope
template<typename Person, typename Teacher>
std::unique_ptr<Person> static_unique_pointer_cast (std::unique_ptr<Teacher>&& old){
    return std::unique_ptr<Person>{static_cast<Person*>(old.release())};
    //conversion: unique_ptr<FROM>->FROM*->TO*->unique_ptr<TO>
}

auto main() -> int {


    auto t1 = std::make_unique<Teacher>();
    auto t2 = std::make_unique<Teacher>();
    auto t3 = std::make_unique<Teacher>();

    auto s1 = std::make_unique<Student>();
    auto s2 = std::make_unique<Student>();
    auto s3 = std::make_unique<Student>();

    std::vector<std::unique_ptr<Person>> v;
    // v.push_back(static_unique_pointer_cast<Person>(std::move(s1)));

    auto foo = static_unique_pointer_cast<Person>(std::move(s1));
    // std::vector<std::unique_ptr<Person>> ve = {
    //     std::move(t1), 
    //     std::move(t2), 
    //     std::move(t3), 
    //     std::move(s1), 
    //     std::move(s2), 
    //     std::move(s3)
    //     };

    return 0;
}

编辑:我通过将基类析构函数更改为默认值来让它工作。

我现在有这个:

std::vector<std::unique_ptr<Person>> v;
v.push_back(static_unique_pointer_cast<Person>(std::move(s1)));
v.push_back(static_unique_pointer_cast<Person>(std::move(s1)));

for (auto item: v) {
    item->printName();
}

但我收到以下错误:

error: call to implicitly-deleted copy constructor of 'std::__1::unique_ptr<Person, std::__1::default_delete<Person> >'
    for (auto item: v) {

编辑 2:

以上在我使用时有效:

for (auto &&item: v) {
        item->printName();
    }

有人能给我解释一下吗?该 vector 包含唯一指针(曾经是右值(特别是 exrvalue),但现在不是了。为什么我需要使用 auto &&?

最佳答案

我已经清理了代码并将尝试解释这些更改。

#include <iostream>
#include <vector>
#include <memory>

class Person {
public:
    virtual ~Person() = default; // before this was a pure-virtual d'tor. Usually, you don't need it, but the linker told you it wasn't implemented

    virtual void printName() = 0;
};

class Teacher : public Person {
public:
    void printName() override {
        std::cout << "Hello My Name is Teacher\n";
    }
    // removed d'tor since you already have a default virtual destructor here
};

class Student : public Person {
public:
    void printName() override {
        std::cout << "Hello My Name Is Student\n";
    }
    // removed d'tor since you already have a default virtual destructor here
};

// removed template this upcasting is almost straight forward.

auto main() -> int {

    auto t1 = std::make_unique<Teacher>();
    auto t2 = std::make_unique<Teacher>();
    auto t3 = std::make_unique<Teacher>();

    auto s1 = std::make_unique<Student>();
    auto s2 = std::make_unique<Student>();
    auto s3 = std::make_unique<Student>();

    std::vector<std::unique_ptr<Person>> v;
    v.push_back(std::move(t1));
    v.push_back(std::move(t2));
    v.push_back(std::move(t3)); // Easy to add Students and Teachers
    v.push_back(std::move(s1));
    v.push_back(std::move(s2));
    v.push_back(std::move(s3));

    for (auto &item: v) { // Taking a reference, `&`, the pointers in `v` still stay there after the loop and can be reused. Don't use `&&` unless you want the use the pointers once only.
        item->printName();
    }

    return 0;
}

关于C++ unique_ptr 从派生到基础的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54717805/

相关文章:

c++ - 获取 Win32 程序以在启动时请求调试器?

c++ - 运算符删除导致堆损坏,而运算符新工作正常

c++ - 使用shared_ptr而不是unique_ptr作为类成员只是为了避免隐式复制构造函数删除是否明智?

c++ - 如何使用字符串返回一个指向对象的唯一指针 vector 的迭代器来查找对象?

c++ - 在 Qt5 代码中禁用旧的(Qt4 风格的)Signal-Slot 语法

c++ - 如何传递 std::unique_ptr ?

c++ - OpenGL 着色器版本错误

C++一次运行2个进程

c++ - 变体:没有用于调用 'get' 的匹配函数

c++ - [[maybe_unused]] 应用于静态数据成员