指向uniq_ptr列表的指针uniq_ptr数组
Array of uniq_ptr pointer pointing to list of uniq_ptr
>搜索遍历,找不到答案,我正在寻找w.r.t.unique_ptr。这是我遇到的一个问题,无法用unique_ptr解决,但我可以用传统方式解决。
我坚持使用指向继承类的指针数组abstract_base_class
抱歉发布整个代码
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <memory>
using namespace std;
// Base is an abstract base class.
class Base {
private:
string myName; // Name of this person
public:
Base(string name) : myName(name) {}
// A pure virtual function with a function body
virtual void hello() const {
cout << "Hello, my name is " << myName
<< ". ";
}
};
class ServiceAgent : public Base {
public:
ServiceAgent(string name) : Base(name) {}
void hello() const {
Base::hello();
cout << "I'm a customer service representative. How may I help you?"
<< endl;
cout << "-----n";
}
};
class Student : public Base {
public:
enum category { FRESHMAN, SOPHOMORE, JUNIOR, SENIOR };
category personType;
Student(string name, category pType) : Base(name) {
personType = pType;
}
void hello() const {
string tmp = enumToString(personType);
Base::hello();
cout << tmp << endl;
cout << "-----n";
}
string enumToString (category c) const
{
string s;
switch (c) {
case FRESHMAN:
s = "I'm a Freshman";
break;
case SOPHOMORE:
s = "I'm a Sophomore";
break;
case JUNIOR:
s = "I'm a Junior";
break;
case SENIOR:
s = "I'm a Senior";
break;
default:
s = "none";
break;
}
return s;
}
};
class CSStudent : public Student {
public:
CSStudent(string name, Student::category type) : Student(name, type) {}
void hello() const {
Base::hello();
cout << "I'm a computer science major.n";
cout << "-----n";
}
};
class BUSStudent : public Student {
public :
BUSStudent(string name, Student::category type) : Student(name, type) {}
void hello() {
Base::hello();
cout << "I'm a business major.n";
cout << "-----n";
}
};
int main() {
ServiceAgent *agentJack = new ServiceAgent("Jacqueline");
Student *studentJack = new Student("Jackson", Student::FRESHMAN);
CSStudent *studentCSS = new CSStudent("Jack", Student::SOPHOMORE);
BUSStudent *studentBus1 = new BUSStudent("Jacky", Student::JUNIOR);
BUSStudent *studentBus2 = new BUSStudent("Joyce", Student::SENIOR);
Base *arr[] = { agentJack, studentJack, studentCSS, studentBus1, studentBus2};
for (Base *var : arr)
{
var->hello();
}
unique_ptr<ServiceAgent> u_agentJack(new ServiceAgent("Jacqueline"));
unique_ptr<Student> u_studentJack(new Student("Jackson", Student::FRESHMAN));
unique_ptr<CSStudent> u_studentCSS(new CSStudent("Jack", Student::SOPHOMORE));
unique_ptr<BUSStudent> u_studentBus1(new BUSStudent("Jacky", Student::JUNIOR));
unique_ptr<BUSStudent> u_studentBus2(new BUSStudent("Joyce", Student::SENIOR));
//unique_ptr<Base*> ptr ; //(new Base*);//{ u_agentJack, u_studentJack, u_studentCSS, u_studentBus1, u_studentBus2 };
//ptr = static_cast<unique_ptr<Base*>>u_agentJack;
return 0;
}
我试过这些,不起作用。
unique_ptr<Base*> ptr (new (Base*[5] { u_agentJack, u_studentJack, u_studentCSS, u_studentBus1, u_studentBus2 });
尝试使用单个条目
//ptr = static_cast<unique_ptr<Base*>>u_agentJack;
将std::unique_ptr<Base*>
更改为std::unique_ptr<Base>
,并将虚拟析构函数添加到Base
。然后,可以像声明Base*
指针数组一样声明unique_ptr<Base>
对象的数组,并将现有的std::unique_ptr
对象std::move()
到数组中。
试试这个:
#include <iostream>
#include <string>
#include <memory>
using namespace std;
// Base is an abstract base class.
class Base {
private:
string myName; // Name of this person
public:
Base(string name) : myName(name) {}
virtual ~Base() {}
// A pure virtual function with a function body
virtual void hello() const {
cout << "Hello, my name is " << myName << ".";
}
};
class ServiceAgent : public Base {
public:
ServiceAgent(string name) : Base(name) {}
void hello() const override {
Base::hello();
cout << " I'm a customer service representative. How may I help you?" << endl;
}
};
class Student : public Base {
public:
enum category { FRESHMAN, SOPHOMORE, JUNIOR, SENIOR };
category personType;
Student(string name, category pType) : Base(name), personType(pType) {}
void hello() const override {
Base::hello();
cout << " " << enumToString(personType) << endl;
}
string enumToString (category c) const
{
switch (c) {
case FRESHMAN:
return "I'm a Freshman";
case SOPHOMORE:
return "I'm a Sophomore";
case JUNIOR:
return "I'm a Junior";
case SENIOR:
return "I'm a Senior";
}
return "";
}
};
class CSStudent : public Student {
public:
CSStudent(string name, Student::category type) : Student(name, type) {}
void hello() const override {
Student::hello();
cout << "I'm a computer science major." << endl;
}
};
class BUSStudent : public Student {
public :
BUSStudent(string name, Student::category type) : Student(name, type) {}
void hello() const override {
Student::hello();
cout << "I'm a business major." << endl;
}
};
int main() {
std::unique_ptr<ServiceAgent> agentJack(new ServiceAgent("Jacqueline"));
std::unique_ptr<Student> studentJack(new Student("Jackson", Student::FRESHMAN));
std::unique_ptr<CSStudent> studentCSS(new CSStudent("Jack", Student::SOPHOMORE));
std::unique_ptr<BUSStudent> studentBus1(new BUSStudent("Jacky", Student::JUNIOR));
std::unique_ptr<BUSStudent> studentBus2(new BUSStudent("Joyce", Student::SENIOR));
std::unique_ptr<Base> arr[] = { std::move(agentJack), std::move(studentJack), std::move(studentCSS), std::move(studentBus1), std::move(studentBus2) };
for (auto &var : arr)
{
var->hello();
cout << "-----" << endl;
}
return 0;
}
现场演示
相关文章:
- CLANG 编译器 说:变量"PTR"可能未初始化
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁
- 将 ptr 传递给 ptr 到 A 作为参数传递给 A 的函数是不好的做法吗?
- 为共享 ptr 向量实现复制 c'tor?
- 字符和整数中 **(ptr+1) 的值差异
- C++:在不中断共享的情况下通过引用传递共享 PTR?
- 如何将派生类从基 ptr 分配给 nlohmann::json
- 引用 std::shared:ptr 以避免引用计数
- 为什么我不能在不进行任何转换的情况下将浮点数放入任何类型的 ptr 中?
- 在调用函数时,ptr** 和 ptr*& 之间是否有区别,或者首选C++?
- 另一种类型的智能ptr,比如具有弱refs的unique_ptr
- 尝试打印出 *ptr++ 的值,以了解它是如何工作的
- 如何控制共享 ptr 引用计数?
- C++中的指针否定 (!ptr == NULL)
- 从const ptr*转换为ptr*时出现问题
- 这是MSVC 2013中具有共享PTR的单例的正确实现吗?
- 对唯一 ptr 无效读取的引用向量
- C++ 类型转换基础 PTR 到派生 PTR 保存在引用类中
- 如何使用非类型参数传递模板化类的 Ref 或 Ptr
- 唯一 ptr 将所有权移动到包含对象的方法