[英]c++ design (multiple inheritance vs. listing relevant data)
I have我有
class Skill
{
public :
virtual void printSkill();
//....
}
class Person : public Skill
{
//...
}
In previous design Person could have one skill per person and I could print it在以前的设计中,每个人可以拥有一项技能,我可以打印出来
Person p;
p.printSkill();
Now we want to add multiple skill and display them.现在我们要添加多个技能并显示它们。 I changed:我变了:
class Person
{
private:
vector<Skill*> m_skills;
pubcic:
void addSkill(Skill *s) { m_skills.push_back(s);}
void printSkills() const {
for auto it = m_skills.begin(); it != m_skills.end(); ++it)
{
it->printSkill();
}
}
}
class Skill1 : public Skill
{
void printSkill() ;
}
class Skill2 : public Skill
{
void printSkill() ;
}
Person p;
Skill1 s1;
Skill2 s2;
p.addSkill(&s1);
p.addSkill(&s2);
This should work But I dont like this.这应该有效但我不喜欢这个。 may be I can solve this without addSkill
method?也许我可以在没有addSkill
方法的情况下解决这个addSkill
? Using MultipleInheitance?使用多重继承?
What's best depends on a large number of factors.什么是最好的取决于很多因素。
With your vector<Skill*>
version:使用您的vector<Skill*>
版本:
Skill
-derived object may be added to multiple Person
objects, which might be memory efficient and desirable, or inappropriate (eg if the Skills objects had to have Person
-specific data members, such as the extent of skill, eg a bench-press skill might want a number of kgs specific to the Person
)相同的Skill
派生对象可以添加到多个Person
对象中,这可能是内存高效且可取的,或者不合适的(例如,如果 Skills 对象必须具有Person
特定的数据成员,例如技能的范围,例如 bench-新闻技能可能需要一些特定于Person
的公斤数)Skills
-derived objects, which must never-the-less outlive any use of them via the pointers to them you're storing in Person
objects, or you'll get undefined behaviour . Skills
派生对象的生命周期没有固有的管理,它必须永远超过通过您存储在Person
对象中的指向它们的指针对它们的任何使用,否则您将获得未定义的行为。 This may be fine: perhaps you can create some set number of skills once in the program lifetime, and not need to destruct them until after all the Person
objects' lifetimes.这可能很好:也许您可以在程序生命周期中创建一定数量的技能一次,并且不需要在所有Person
对象的生命周期结束后销毁它们。 At other times, the calling code will have take extra responsibility and care.在其他时候,调用代码将承担额外的责任和照顾。Other possible approaches include:其他可能的方法包括:
Having Person
store a vector<unique_ptr<Skill>>
, and have client code hand over ownership to the skills objects it adds: addSkill(std::make_unique<SomeSkill>(constructor, args, la, de, da);
让Person
存储vector<unique_ptr<Skill>>
,并让客户端代码将所有权移交给它添加的技能对象: addSkill(std::make_unique<SomeSkill>(constructor, args, la, de, da);
You could also have a using SomeSkill = std::variant<Skill1, Skill2, Skill3>;
你也可以using SomeSkill = std::variant<Skill1, Skill2, Skill3>;
and have Person
store a std::vector<SomeSkill>
.并让Person
存储一个std::vector<SomeSkill>
。 This achieves something similar to the last, but using a buffer inside the variant big enough to store the largest Skill
-derived object, instead of storing pointers off to dynamically allocated Skill
objects.这实现了与上一个类似的东西,但是在变体中使用了一个足够大的缓冲区来存储最大的Skill
派生对象,而不是存储指向动态分配的Skill
对象的指针。
You could use multiple inheritance, but then you'd have multiple base classes providing a same-named function, and to call a specific one you have to disambiguate that at the time.您可以使用多重继承,但是您将有多个基类提供同名函数,并且要调用特定的函数,您必须在当时消除歧义。 If you want to be able to easily declare Person
s with different skills, and still have a printSkills
function that automatically discovers and prints the individual skills from the multiple bases, that's possible using variadic templates and parameter packs, but judging from the question - respectfully - you're not ready to go down that route.如果您希望能够轻松声明具有不同技能的Person
,并且仍然有一个printSkills
功能可以自动从多个基础中发现和打印单个技能,那么可以使用可变参数模板和参数包,但是从问题来看 - 恭敬- 你还没准备好走那条路。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.