到处搜索,找不到答案我在找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;
答案 0 :(得分:0)
将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;
}