呵呵,這裏的每個人都說「不」。我說「是的,它確實有意義。」
class VirtualBase {
public:
virtual void vmethod() = 0;
// If "global" is an instance of Concrete, then you can still access
// VirtualBase's public members, even though they're private members for Concrete
static VirtualBase *global;
};
// This can also access all of VirtualBase's public members,
// even if an instance of Concrete is passed in,
void someComplicatedFunction(VirtualBase &obj, ...);
class Concrete : private VirtualBase {
private:
virtual void vmethod();
public:
void cmethod() {
// This assignment can only be done by Concrete or friends of Concrete
VirtualBase::global = this;
// This can also only be done by Concrete and friends
someComplicatedFunction(*this);
}
};
製作繼承private
並不意味着你不能從類外部訪問的VirtualBase
成員,它只是意味着你無法通過參考訪問這些成員Concrete
。但是,Concrete
及其朋友可以將Concrete
的實例投射到VirtualBase
,然後任何人都可以訪問公共成員。簡單地說,
Concrete *obj = new Concrete;
obj->vmethod(); // error, vmethod is private
VirtualBase *obj = VirtualBase::global;
obj->vmethod(); // OK, even if "obj" is really an instance of Concrete
我不說「不」。看到我的答案。 :) – iammilind