说明,在这里决定跳过第二章,实在是因为里面涉及的内容太理论,对我而言又太艰深

3.1 HANDLES AND OBJECT-ORIRNTED PROGRAMMING

In normal object-oriented programming practice,information hiding is achieved by declaring certain members as private or protected,so the client side code can't access them directly.But the compiler still needs to know perfectly well all members,their types,names,and orders in a class.At least,the compiler needs to know the exact size of an instance of an object for memory allocation.This can cause lots of problems for the modular development of programs,Every time a data member or member function is changed,the whole program needs to be recompiled.Programs complied with older versions of class definition would not work with newer version.To solve this problem,there is the abstract bass class.The abstract bass class,which uses virtual functions to define the interface the clien-side program can see while completely hiding away the implementation,improves information hiding and the modularity of programs even further...For hiding the implementation away from the client side of a class, normally a special function is provided to create an instance of a derived class,including memory allocation; another special function is provided to destroy an instance,including freeing its memory.

Objects in the Win32 API can be seen as being implemented using abstract base class with no data members. The data representation of an object is completely hidden from the user application...the perfect information hiding provided by the Win32 API greatly improves the portability of programs. GDI normally provides several functions to create an instance of an object and several functions to destroy them.

To illustrate our comparison between object-roiented programming and the Win32 API,let's try to provide some minimum pseudo-implementation of GDI using C++.

//gdi.h

#include<windows.h>
class _GdiObj
{
public:virtual int GetObjectType(void) = 0;virtual int GetObject(int cbBuffer, void * pBuffer) =0;virtual bool DeleteObject(void) = 0;virtual bool UnrealizeObject(void) = 0;
};class _Pen:public _GdiObj
{
public:virtual int GetObjectType(void){return OBJ_PEN;}virtual int GetObject(int cbBuffer,void *pBuff)=0;virtual bool DeleteObject(void)=0;virtual bool UnrealizeObject(void){return true;}
};_Pen * _CreatePen(int fnPenStyle, int nWidth, COLORREF crColor);

//gdi.cpp#define STRICT
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "gdi.h"
class _RealPen : public _Pen
{LOGPEN m_LogPen;
public:_RealPen(int fnPenStyle, int nWidth, COLORREF crColor){m_LogPen.lopnStyle = fnPenStyle;m_LogPen.lopnWidth.x = nWidth;m_LogPen.lopnWidth.y = 0;m_LogPen.lopnColor = crColor;}int GetObject(int cbBuffer, void * pBuffer){if ( pBuffer==NULL )return sizeof(LOGPEN);else if ( cbBuffer>=sizeof(m_LogPen) ){memcpy(pBuffer, & m_LogPen, sizeof(m_LogPen));return sizeof(LOGPEN);}else{SetLastError(ERROR_INVALID_PARAMETER);return 0;}}bool DeleteObject(void){if ( this ){delete this;return true;}elsereturn false;}
};
_Pen * _CreatePen(int fnPenStyle, int nWidth, COLORREF crColor)
{return new _RealPen(fnPenStyle, nWidth, crColor);
}

//test.cpp

#include "gdi.h"void Test(void)
{_Pen * pPen = _CreatePen(PS_SOLID, 1, RGB(0, 0, 0xFF));////pPen->DeleteObject();
}
int WINAPI WinMain(HINSTANCE hInsatcne,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{return 0;
}

这个程序定义_GdiObj这样一个抽象基类,紧接着派生出_Pen类,同为抽象基类,在_Pen类的子类_RealPen中才将纯虚函数一一实现。另外从这个程序中也可以看出,创建画笔对象调用的函数,其实是填充一个LOGPEN结构类型数据的几个字段

转载于:https://www.cnblogs.com/lanf/p/5094385.html

Chapter 3.GDI/DirectDraw Internal Data Structures相关推荐

  1. Data Structures with C++ Using STL Chapter 3算法概述---笔记

    <Data Structures with C++ Using STL Chapter 3算法概述---笔记>,作者:茉莉花茶,原文链接:http://www.cnblogs.com/yc ...

  2. Persistent Data Structures(可持久化的数据结构)

    Persistent Data Structures 可持久化的数据结构 Contents 内容 Introduction                          介绍 Persistent ...

  3. 数据结构头文件《Fundamentals of Data Structures in C (2nd Edition) 》/严蔚敏版《数据结构》

    #ifndef _PUBLIC_H_ #define _PUBLIC_H_#include <malloc.h>//动态分配储存空间 #include <stdlib.h> # ...

  4. pandas笔记(pandas Data Structures)

    pandas笔记(pandas Data Structures) 生信start_site已关注 32020.06.15 03:02:37字数 766阅读 509 pandas包含数据结构和数据操作工 ...

  5. python 科学计算设计_Python科学计算——Data Structures

    为什么选择Python作为科学计算语言? 有关于Matlab和Python哪个更适合作为科学计算语言的争论已久,之所以选择Python作为首选的科学计算语言,不仅仅是因为它免费,开源,有很多优秀的库和 ...

  6. 20162314 《Program Design Data Structures》Learning Summary Of The First Week

    20162314 2017-2018-1 <Program Design & Data Structures>Learning Summary Of The First Week ...

  7. 【Python学习笔记】Coursera课程《Python Data Structures》 密歇根大学 Charles Severance——Week6 Tuple课堂笔记...

    Coursera课程<Python Data Structures> 密歇根大学 Charles Severance Week6 Tuple 10 Tuples 10.1 Tuples A ...

  8. 2014 UESTC Training for Data Structures D - 长使英雄泪满襟

    以下内容来自ShallWe's blog 题目 2014 UESTC Training for Data Structures D - 长使英雄泪满襟 看出司马懿在等蜀军粮草不济,孔明于是下令分兵屯田 ...

  9. Data Structures[翻译]

    Data Structures                                      [原文见:http://www.topcoder.com/tc?module=Static&a ...

最新文章

  1. 仅通过崩溃地址找出源代码的出错行
  2. Windows2003 SQL2005解决系统Administrator密码不知道的问题
  3. everything安装版和便携版有什么区别_A2奶粉内幕:国行版和澳洲版有什么区别 贵的不一定好!...
  4. java中显示动态信息的方法_java里的动态表单技术
  5. docker run 挂载卷_docker mysql配置挂载到卷
  6. 面向对象编程(一):类对象
  7. linux编译器下载地址,GNU Compiler Collection(gcc编译器) v4.9.1 linux版
  8. 数据挖掘技术有哪几种
  9. java me手机版,一个经典的 JAVA ME 手机程序入门级源码
  10. 排序算法积累(3)-----快速排序
  11. 在Linux中smbfs文件系统的挂载
  12. U盘被写保护,无法格式化
  13. 嵌入式系统开发笔记33:关于“人生苦短,我学Python”这句话的由来
  14. 矩阵快速幂(共轭函数)
  15. oracle缓冲区闩锁类型,等待缓冲区闩锁时出现超时 -- 类型 4
  16. R语言也可以进行ATAC数据的完整分析啦!
  17. ajax初始化 ztree v3,zTree_v3
  18. sharemouse切窗口就锁定了什么原因_使各大网课软件监控功能和锁定功能“失效”...
  19. 史上最全 2019 ICRA顶会四足机器人文献整理
  20. 关于Trunk封装的协议和模式。如何配置trunk

热门文章

  1. Vue移动端项目——搜索联想建议功能的实现(结合watch属性和使用lodash防抖节流)
  2. Vue项目中使用图片裁切器 cropperjs (头像裁切)
  3. Web框架——Flask系列之Flask-SQLAlchemy安装与使用 定义数据模型(八)
  4. LeetCode MySQL 1581. 进店却未进行过交易的顾客
  5. 剑指Offer - 面试题45. 把数组排成最小的数(字符串排序)
  6. 聚类算法 距离矩阵_快速且不需要超参的无监督聚类方法
  7. Codeforces Round #701 (Div. 2)赛后补题报告(A~D)
  8. linux git还原文件,Gitlab备份到windows、在Linux恢复
  9. 直击行业痛点!端侧模型部署的成熟解决方案有了!
  10. 聊聊如何提升推荐系统的结果多样性