【发布时间】:2016-05-29 12:05:09
【问题描述】:
鉴于这种情况:
class GrandParent {};
class Parent : public GrandParent {};
class Child : public Parent {}; /// Ok
class Child : public GrandParent {}; /// Is it possible to force a compilation error?
【问题讨论】:
-
如果它的唯一目的是作为
Parent的基类,为什么GrandParent存在? -
@PeteBecker 对于“正常”继承可能没有理由,但对于技巧,当然(base-from-member idiom,EBO,common members for main class and its specialization ...这个列表可以继续, 和上)。对于现实生活中的示例,请考虑 libstdc++ 的
std::vector -
@milleniumbug - 此继承层次结构中没有任何内容表明它是用于“技巧”的。如果是,问题应该这样说,以便得到更合适的答案。
-
@PeteBecker,正如 milleniumbug 所说,它旨在制造一些技巧。这不是我想要的层次结构;这只是其中的一部分。但这对你来说是无关紧要的信息,我可以这么说,因为你是唯一一个抱怨过这个的人;没有冒犯的伙伴。还是谢谢你!
标签: c++ inheritance