指向uniq_ptr列表的指针uniq_ptr数组

Array of uniq_ptr pointer pointing to list of uniq_ptr

本文关键字:ptr uniq 数组 指针 列表 指向      更新时间:2023-10-16

>搜索遍历,找不到答案,我正在寻找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;
}

现场演示