C unique_ptr从派生到基础铸造
C++ unique_ptr casting from derived to base
我一直在阅读,并努力理解和实现此功能。
我有一个基础班级,是老师&学生继承。我想将它们都放在"人"类型的矢量中。我尝试了一些事情。但是,我不断遇到错误页面,我正在努力理解它们。
此当前代码用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();
}
有人可以向我解释一下吗?矢量具有独特的指针(曾经是rvalue(特别是Exrvalue),但现在不是。为什么我需要使用自动&amp;?
我已经清理了代码,请尝试解释更改。
#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 Teachern";
}
// 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 Studentn";
}
// 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;
}
相关文章:
- 为什么使用 "this" 指针调用派生成员函数?
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 为什么 std::unique 不调用 std::sort?
- 在派生函数中指定void*参数
- 如何通过派生类函数更改基类中的向量
- 如何委托派生类使用其父构造函数?
- 如何使用单独文件中的派生类访问友元函数对象
- 派生类销毁的最佳实践是什么
- 如何使用基类指针引用派生类成员
- 派生类是否可以在抽象工厂设计模式中具有数据成员
- 使用基类指针创建对象时,缺少派生类析构函数
- 如何引用基类的派生类?
- 存储模板类型以强制转换回派生<T>
- 需要从 istream 和 ostream 派生 iostream
- 在 C++ 中用派生类型重写成员函数
- 具有多个类、派生类的C++正向声明
- 有没有一种"cleaner"的方法可以在指向基的指针向量中找到派生类的第一个实例?
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 如果基类包含双指针成员,则派生类的构造函数
- 为什么此派生对象无法访问基类的后递减方法?