首页 > 解决方案 > 指向 uniq_ptr 列表的 uniq_ptr 指针数组

问题描述

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

标签: c++arraysunique-ptr

解决方案


更改std::unique_ptr<Base*>std::unique_ptr<Base>,并将虚拟析构函数添加到Base. 然后,您可以像声明指针unique_ptr<Base>数组一样声明对象数组Base*,并将std::move()现有std::unique_ptr对象放入数组中。

试试这个:

#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;
}

现场演示


推荐阅读