Chinaunix首页 | 论坛 | 博客
  • 博客访问: 271426
  • 博文数量: 55
  • 博客积分: 2030
  • 博客等级: 大尉
  • 技术积分: 737
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-13 18:06
文章分类

全部博文(55)

文章存档

2011年(2)

2010年(7)

2009年(17)

2008年(29)

我的朋友

分类: C/C++

2008-09-13 20:56:23

compilers will generally not convert a derived class object (such as Student) into a base class object (such as Person) if the inheritance relationship between the classes is private.
 
The second rule is that members inherited from a private base class become private members of the derived class, even if they were protected or public in the base class.
 
Private inheritance means is-implemented-in-terms-of. private inheritance means that implementation only should be inherited; interface should be ignored
 
use composition whenever you can, and use private inheritance whenever you must.
 
 
derived classes may redefine virtual functions even if they are not permitted to call them
 
 

class Empty {}; // has no data, so objects should



class HoldsAnInt: private Empty {

private:

  int x;

};

you're almost sure to find that sizeof(HoldsAnInt) == sizeof(int). This is known as the empty base optimization (EBO)

Private inheritance is most likely to be a legitimate design strategy when you're dealing with two classes not related by is-a where one either needs access to the protected members of another or needs to redefine one or more of its virtual functions.

Item [40]

C++ takes no position on this debate. It happily supports both options, though its default is to perform the replication. If that's not what you want, you must make the class with the data (i.e., File) a virtual base class.

My advice on virtual base classes (i.e., on virtual inheritance) is simple. First, don't use virtual bases unless you need to. By default, use non-virtual inheritance. Second, if you must use virtual base classes, try to avoid putting data in them.

 

 

 

阅读(638) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~