Inside the C++ Object Model 读书笔记(一)

Chapter 1 Object Lessons

In C, there is no language-supported relationship between data and functions. We speak of this method of programming as procedural.

  • Layout Costs for Adding Encapsulation
    The member functions, although included in the class declaration, are not reflected in the object layout; one copy only of each non-inline member function is generated. the primary layout and access-time overheads within C++ are associated with the virtuals, that is:
    1. the virtual function mechanism in its support of an efficient run-time binding
    2. a virtual base class in its support of a single, shared instance of a base class occurring multiple times within an inheritance hierarchy.

1.1 The C++ Object Model

Two flavors of class member data: static, nonstatic.Three flavors of class member function: static, nonstatic,

  • simple object model

an object is a sequence of slots, where each slot points to a member

image

this simple concept of an index or slot number is the one that has been developed into the C++ pointer-to-member concept

  • table-driven object model

The class object contains the pointers to the two member tables, the one is member data table and the other is member function table.

image

Although this model is not used in practice within C++, the concept of a member function table has been the traditional implementation supporting efficient runtime resolution of virtual functions

  • The C++ Object Model

Nonstatic data members are allocated directly within each class object. Virtual functions are supported in two steps:

  1. A table of pointers to virtual functions is generated for each class (this is called the virtual table).
  2. A single pointer to the associated virtual table is inserted within each class object (traditionally, this has been called the vptr). The setting, resetting, and not setting of the vptr is handled automatically through code generated within each class constructor, destructor, and copy assignment operator (this is discussed in Chapter 5). The type_info object associated with each class in support of runtime type identification (RTTI) is also addressed within the virtual table, usually within the table’s first slot.

Image

  • Adding Inheritance

1.2 A Keyword Distinction

One reasonable use of the C struct in C++, then, is when you want to pass all or part of a complex class object to a C function. This struct declaration serves to encapsulate that data and guarantees a compatible C storage layout.

struct : default public, can’t use in template as class.
class : defautl private, template key word.

struct C_point { ... }; 

class Point { 
public: 
   operator C_point() { return _c_point; } 
   // ... 
private: 
   C_point _c_point; 
   // ... 
}; 

1.3 An Object Distinction

The C++ programming model directly supports three programming paradigms.

  1. The procedural model as programmed in C
char boy[] = "Danny"; 
char *p_son; 

... 

p_son = new char[ strlen( boy ) + 1 ]; 
strcpy( p_son, boy ); 

... 

if ( !strcmp( p_son, boy )) 
   take_to_disneyland( boy ); 
  1. The abstract data type (ADT) model
String girl = "Anna"; 
String daughter; 

... 

// String::operator=(); 
daughter = girl; 

...
// String::operator==(); 
if ( girl == daughter ) 
   take_to_disneyland( girl ); 
  1. The object-oriented (OO) model
void 
check_in( Library_materials *pmat ) 
{ 
   if ( pmat->late() ) 
        pmat->fine(); 
   pmat->check_in(); 
   if ( Lender *plend = pmat->reserved() ) 
        pmat->notify( plend ); 
} 

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(); 
  1. Through the virtual function mechanism
ps->rotate(); 
  1. Through the dynamic_cast and typeid operators
if ( circle *pc = 
   dynamic_cast< circle* >( ps )) ... 

The memory requirements to represent a class object in general are the following:

  • The accumulated size of its nonstatic data members

  • Plus any padding (between members or on the aggregate boundary itself) due to alignment constraints (or simple efficiency)

  • Plus any internally generated overhead to support the virtuals

1.4 Memory Layout

class Bear : public ZooAnimal { 
public: 
   Bear(); 
   ~Bear(); 
   // ... 
   void rotate(); 
   virtual void dance(); 
   // ... 
protected: 
   enum Dances { ... }; 

   Dances dances_known; 
   int cell_block; 
}; 

Bear b( "Yogi" ); 
Bear *pb = &b; 
Bear &rb = *pb; 

image

{ 
   ZooAnimal za; 
   ZooAnimal *pza; 

   Bear b; 
   Panda *pp = new Panda; 

   pza = &b; 
} 

image

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.

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值