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

全部博文(55)

文章存档

2011年(2)

2010年(7)

2009年(17)

2008年(29)

我的朋友

分类: C/C++

2008-09-26 00:22:40

the primary layout and access-time overheads within C++ are associated with the virtuals.
There is also additional overhead under multiple inheritance in the conversion between a derived class and its second or subsequent base class.
 
multiple inheritance:
 

// original pre-Standard iostream implementation
class iostream:
   public istream,
   public ostream { ... };

virtual inheritance

class istream : virtual public ios { ... };
class ostream : virtual public ios { ... };

In the case of virtual inheritance, only a single occurrence of the base class is maintained (called a subobject) regardless of how many times the class is derived from within the inheritance chain. iostream, for example, contains only a single instance of the virtual ios base class.

Each class object contains a bptr initialized to address its base class table

A meta-language rule is required, dictating that when the language cannot distinguish between a declaration and an expression, it is to be interpreted as a declaration.

In C++, polymorphism exists only within individual public class hierarchies.
 

The C++ language supports polymorphism in the following ways:

  1. Through a set of implicit conversions, such as the conversion of a derived class pointer to a pointer of its public base type:

    shape *ps = new circle(); 

  2. Through the virtual function mechanism:

    ps->rotate(); 

  3. Through the dynamic_cast and typeid operators:

    if ( circle *pc = 
       dynamic_cast< circle* >( ps )) ... 
an observant compiler can resolve an invocation of a virtual function through the object at compile time, thus by-passing the virtual mechanism. This can be a significant performance win if the virtual function is defined as inline.

To summarize, polymorphism is a powerful design mechanism that allows for the encapsulation of related types behind an abstract public interface, such as our Library_materials hierarchy. The cost is an additional level of indirection, both in terms of memory acquisition and type resolution. C++ supports polymorphism through class pointers and references. This style of programming is called object-oriented.

 

C++ also supports a concrete ADT style of programming now called object-based (OB)—nonpolymorphic data types, such as a String class. A String class exhibits a nonpolymorphic form of encapsulation;

 

There are four characteristics of a class under which the compiler needs to synthesize a default constructor for classes that declare no constructor at all.

    Programmers new to C++ often have two common misunderstandings:

    1. That a default constructor is synthesized for every class that does not define one

    2. That the compiler-synthesized default constructor provides explicit default initializers for each data member declared within the class

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