Массив указателя unique_ptr, указывающего на список unique_ptr

Обратите внимание на класс «MAIN», в который помещается элемент, например

<div class="container">
     <ul class="select">
         <li> First</li>
         <li>Second</li>
    </ul>
</div>

. В приведенном выше сценарии объект MAIN, который будет наблюдать jQuery, является «контейнером».

Тогда вы в основном будете иметь имена элементов в контейнере, такие как ul, li и select:

$(document).ready(function(e) {
    $('.container').on( 'click',".select", function(e) {
        alert("CLICKED");
    });
 });
-4
задан Remy Lebeau 28 March 2019 в 01:54
поделиться

1 ответ

Измените 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;
}

Live Demo

0
ответ дан Remy Lebeau 28 March 2019 в 01:54
поделиться
Другие вопросы по тегам:

Похожие вопросы: