私有继承VS组成:什么时候使用? [英] Private inheritance VS composition : when to use which?

查看:117
本文介绍了私有继承VS组成:什么时候使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

私人继承VS组合。



我使用每一个时有点混乱。由于私人继承在某种程度上封闭了继承,因此:

  class A 
{
private:
int z;
protected:
int y;
public:
int x;
};

class B:private A
{
/ * B的数据成员和方法* /
/ * B只能访问A的public和protected * /
};

class C:public B
{
/ *可以访问没有B * /
}的字段;

C 将无法使用 B 的任何字段。什么时候使用私有继承,何时使用组合?



谢谢!

解决方案

此C ++常见问题解答条目


p>使用组合,当你可以,私人继承,当你有。


通常你不想访问内部的太多其他类,和私人继承给你一些额外的权力(和责任)。但私人继承不是邪恶的;



私有继承的一个合法的长期使用是,当一个合法的,长期的使用私有继承的时候,您想要构建使用类Wilma 中的代码的类Fred ,以及 class Wilma 需要从您的新类 Fred 调用成员函数。在这种情况下, Fred 调用 Wilma Wilma 调用(通常是纯虚拟)本身,它们被 Fred 覆盖。这对组合来说要难得多。

 类Wilma {
protected:
void fredCallsWilma )
{
std :: cout<< Wilma :: fredCallsWilma()\\\
;
wilmaCallsFred();
}
virtual void wilmaCallsFred()= 0; //纯虚函数
};

class Fred:private Wilma {
public:
void barney()
{
std :: cout< Fred :: barney()\\\
;
Wilma:fredCallsWilma();
}
protected:
virtual void wilmaCallsFred()
{
std :: cout< Fred :: wilmaCallsFred()\\\
;
}
};


Private inheritance VS composition.

I'm having a little confusion when to use each. Since private inheritance seals, in a way, the chain on inheritance, given:

class A
{
private:
    int z;
protected:
    int y;
public:
    int x;
};

class B : private A
{
    /* B's data members and methods */
    /* B has access only to A's public and protected */
};

class C : public B
{
    /* can access no fields of B */
};

C won't be able to use any of B's fields. When would I use private inheritance, and when would I use composition?

thanks!

解决方案

This C++ FAQ entry answers your questions aptly.

Replicating it here:

Use composition when you can, private inheritance when you have to.

Normally you don't want to have access to the internals of too many other classes, and private inheritance gives you some of this extra power (and responsibility). But private inheritance isn't evil; it's just more expensive to maintain, since it increases the probability that someone will change something that will break your code.

A legitimate, long-term use for private inheritance is when you want to build a class Fred that uses code in a class Wilma, and the code from class Wilma needs to invoke member functions from your new class, Fred. In this case, Fred calls non-virtuals in Wilma, and Wilma calls (usually pure virtuals) in itself, which are overridden by Fred. This would be much harder to do with composition.

class Wilma {
 protected:
   void fredCallsWilma()
     {
       std::cout << "Wilma::fredCallsWilma()\n";
       wilmaCallsFred();
     }
   virtual void wilmaCallsFred() = 0;   // A pure virtual function
 };

 class Fred : private Wilma {
 public:
   void barney()
     {
       std::cout << "Fred::barney()\n";
       Wilma::fredCallsWilma();
     }
 protected:
   virtual void wilmaCallsFred()
     {
       std::cout << "Fred::wilmaCallsFred()\n";
     }
 };

这篇关于私有继承VS组成:什么时候使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆