如何更改唯一指针向量的可见性
how to change the visibility of a vector of unique pointers
在 ONG class 中,我创建了一个添加功能,可以在参与者向量中添加参与者(主管、管理员、员工)。
std::vector<unique_ptr<Participant>> ls;
我试图让向量作为 public 变量失效,但没有成功
当我想在函数中添加时,一切正常,
但是当我将列表置于功能之外时,它给了我一个错误
class ONG : public Participant {
private:
public:
std::vector<unique_ptr<Participant>> ls;
ONG() = default;
void add(unique_ptr<Participant> part) {
part->tipareste();
ls.emplace_back(std::move(part));
for (const auto& i : ls) {
i->tipareste();
}
}
};
完整代码如下:
#include <iostream>
#include <assert.h>
#include <vector>
#include <memory>
#include <variant>
#define _CRTDBG_MAP_ALLOC
#include <cstdlib>
#include <crtdbg.h>
using namespace std;
struct AtExit
{
~AtExit() { _CrtDumpMemoryLeaks(); }
} doAtExit;
class Participant {
public:
virtual void tipareste() = 0;
bool eVoluntar = true;
virtual ~Participant() = default;
};
class Personal : public Participant {
private:
string nume;
public:
Personal() = default;
Personal(string n) :nume{ n } {}
void tipareste() override {
cout << nume;
}
};
class Administrator : public Personal {
public:
std::unique_ptr<Personal> pers;
Administrator() = default;
Administrator(Personal* p) : pers{ p } {}
void tipareste() override {
cout << "administrator ";
pers->tipareste();
}
};
class Director : public Personal {
public:
std::unique_ptr<Personal> pers;
Director() = default;
Director(Personal* p) : pers{ p } {}
void tipareste() override {
cout << "director ";
pers->tipareste();
}
};
class Angajat :public Participant {
public:
std::unique_ptr<Participant> participant;
Angajat() = default;
Angajat(Participant* p) : participant{ p } { this->eVoluntar = false; /*p->eVoluntar = false;*/ }
void tipareste() override {
cout << "anjajat ";
participant->tipareste();
}
};
class ONG : public Participant {
private:
public:
ONG() = default;
std::vector<unique_ptr<Participant>> ls;
void add(unique_ptr<Participant> part) {
ls.emplace_back(std::move(part));
}
};
int main() {
ONG* ong{};
std::unique_ptr<Participant> aba = std::unique_ptr<Personal>(new Personal("Will"));
ong->add(std::move(aba));
}
问题是 class ONG
是一个抽象 class 因为它继承自具有纯虚函数的 Participant。
如果你定义
void tipareste() {
//Do stuff
}
在 ONG
内(或在 ONG
继承的 class 内)
然后将 ONG
对象分配给 ONG
指针
int main() {
std::shared_ptr<ONG> ong = std::make_shared<ONG> ();
std::unique_ptr<Participant> aba = std::unique_ptr<Personal>(new Personal("Will"));
ong->add(std::move(aba));
}
可以是运行
在 ONG class 中,我创建了一个添加功能,可以在参与者向量中添加参与者(主管、管理员、员工)。
std::vector<unique_ptr<Participant>> ls;
我试图让向量作为 public 变量失效,但没有成功
当我想在函数中添加时,一切正常, 但是当我将列表置于功能之外时,它给了我一个错误
class ONG : public Participant {
private:
public:
std::vector<unique_ptr<Participant>> ls;
ONG() = default;
void add(unique_ptr<Participant> part) {
part->tipareste();
ls.emplace_back(std::move(part));
for (const auto& i : ls) {
i->tipareste();
}
}
};
完整代码如下:
#include <iostream>
#include <assert.h>
#include <vector>
#include <memory>
#include <variant>
#define _CRTDBG_MAP_ALLOC
#include <cstdlib>
#include <crtdbg.h>
using namespace std;
struct AtExit
{
~AtExit() { _CrtDumpMemoryLeaks(); }
} doAtExit;
class Participant {
public:
virtual void tipareste() = 0;
bool eVoluntar = true;
virtual ~Participant() = default;
};
class Personal : public Participant {
private:
string nume;
public:
Personal() = default;
Personal(string n) :nume{ n } {}
void tipareste() override {
cout << nume;
}
};
class Administrator : public Personal {
public:
std::unique_ptr<Personal> pers;
Administrator() = default;
Administrator(Personal* p) : pers{ p } {}
void tipareste() override {
cout << "administrator ";
pers->tipareste();
}
};
class Director : public Personal {
public:
std::unique_ptr<Personal> pers;
Director() = default;
Director(Personal* p) : pers{ p } {}
void tipareste() override {
cout << "director ";
pers->tipareste();
}
};
class Angajat :public Participant {
public:
std::unique_ptr<Participant> participant;
Angajat() = default;
Angajat(Participant* p) : participant{ p } { this->eVoluntar = false; /*p->eVoluntar = false;*/ }
void tipareste() override {
cout << "anjajat ";
participant->tipareste();
}
};
class ONG : public Participant {
private:
public:
ONG() = default;
std::vector<unique_ptr<Participant>> ls;
void add(unique_ptr<Participant> part) {
ls.emplace_back(std::move(part));
}
};
int main() {
ONG* ong{};
std::unique_ptr<Participant> aba = std::unique_ptr<Personal>(new Personal("Will"));
ong->add(std::move(aba));
}
问题是 class ONG
是一个抽象 class 因为它继承自具有纯虚函数的 Participant。
如果你定义
void tipareste() {
//Do stuff
}
在 ONG
内(或在 ONG
继承的 class 内)
然后将 ONG
对象分配给 ONG
指针
int main() {
std::shared_ptr<ONG> ong = std::make_shared<ONG> ();
std::unique_ptr<Participant> aba = std::unique_ptr<Personal>(new Personal("Will"));
ong->add(std::move(aba));
}
可以是运行