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

全部博文(55)

文章存档

2011年(2)

2010年(7)

2009年(17)

2008年(29)

我的朋友

分类: C/C++

2008-09-10 00:26:53

 Private inheritance means something entirely different (see ), and protected inheritance is something whose meaning eludes me to this day
 
 there is no one ideal design for all software. The best design depends on what the system is expected to do
 
The is-a relationship is not the only one that can exist between classes. Two other common inter-class relationships are "has-a" and "is-implemented-in-terms-of."
 
Item[33]
This means that if you inherit from a base class with overloaded functions and you want to redefine or override only some of them, you need to include a using declaration for each name you'd otherwise be hiding.

class Base {
private:
  int x;
public:
  virtual void mf1() = 0;
  virtual void mf1(int);
  virtual void mf2();
  void mf3();
  void mf3(double);
  ...
};

class Derived: public Base {
public:
  using Base::mf1; // make all things in Base named mf1 and mf3
  using Base::mf3; // visible (and public) in Derived's scope


  virtual void mf1();
  void mf3();
  void mf4();
  ...
};

a using declaration makes all inherited functions with a given name visible in the derived class.

 

class Base {
public:
  virtual void mf1() = 0;
  virtual void mf1(int);
  ... // as before
};

class Derived: private Base {
public:
  virtual void mf1() // forwarding function; implicitly
  { Base::mf1(); } // inline (see Item 30)
  ...
};

...

Derived d;
int x;
d.mf1(); // fine, calls Derived::mf1
d.mf1(x); // error! Base::mf1() is hidden

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