Chapter 3.GDI/DirectDraw Internal Data Structures
说明,在这里决定跳过第二章,实在是因为里面涉及的内容太理论,对我而言又太艰深
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) = ;
virtual int GetObject(int cbBuffer, void * pBuffer) =;
virtual bool DeleteObject(void) = ;
virtual bool UnrealizeObject(void) = ;
}; class _Pen:public _GdiObj
{
public:
virtual int GetObjectType(void)
{
return OBJ_PEN;
}
virtual int GetObject(int cbBuffer,void *pBuff)=;
virtual bool DeleteObject(void)=; 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 = ;
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 ;
}
}
bool DeleteObject(void)
{
if ( this )
{
delete this;
return true;
}
else
return 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, , RGB(, , 0xFF));
////
pPen->DeleteObject();
}
int WINAPI WinMain(HINSTANCE hInsatcne,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
return ;
}
这个程序定义_GdiObj这样一个抽象基类,紧接着派生出_Pen类,同为抽象基类,在_Pen类的子类_RealPen中才将纯虚函数一一实现。另外从这个程序中也可以看出,创建画笔对象调用的函数,其实是填充一个LOGPEN结构类型数据的几个字段
Chapter 3.GDI/DirectDraw Internal Data Structures的更多相关文章
- Clean Code – Chapter 6 Objects and Data Structures
Data Abstraction Hiding implementation Data/Object Anti-Symmetry Objects hide their data behind abst ...
- 20182320《Program Design and Data Structures》Learning Summary Week9
20182320<Program Design and Data Structures>Learning Summary Week9 1.Summary of Textbook's Con ...
- [轉]Linux Data Structures
Table of Contents, Show Frames, No Frames Chapter 15 Linux Data Structures This appendix lists the m ...
- Persistent Data Structures
原文链接:http://www.codeproject.com/Articles/9680/Persistent-Data-Structures Introduction When you hear ...
- Choose Concurrency-Friendly Data Structures
What is a high-performance data structure? To answer that question, we're used to applying normal co ...
- Objects and Data Structures
Date Abstraction Hiding implementation is not just a matter of putting a layer of fucntions between ...
- Persistent and Transient Data Structures in Clojure
此文已由作者张佃鹏授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 最近在项目中用到了Transient数据结构,使用该数据结构对程序执行效率会有一定的提高.刚刚接触Trans ...
- Operating system management of address-translation-related data structures and hardware lookasides
An approach is provided in a hypervised computer system where a page table request is at an operatin ...
- Important Abstractions and Data Structures
For Developers > Coding Style > Important Abstractions and Data Structures 目录 1 TaskRunne ...
随机推荐
- webfrrm基础
一.B/S和C/S 1.C/S C/S 架构是一种典型的两层架构,其全程是Client/Server,即客户端服务器端架构,其客户端包含一个或多个在用户的电脑上运行的程序,而服务器端有两种,一种是数据 ...
- Linux系统程序的运行级别
Linux系统有7个运行级别: 运行级别 描述 0 系统停机状态,系统默认运行级别不能设为0,否则不能正常启动 1 但用户工作状态,root权限,用于系统维护,禁止远程登录 2 多用户状态(没有NFS ...
- P53 T3
为方便旅客,某航空公司拟开发一个机票预定系统.旅行社把预定机票的旅客信息(姓名.性别.工作单位.身份证号码.旅行时间.旅行目的地等)输入进入该系统,系统为旅客安排航班,印出取票通知和账单,旅客在飞机起 ...
- Jquery做全选
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- mysql数据库及oracle数据库的定时备份
一.oracle数据库备份bat文件 @echo off md "%date:~0,10%" cd "%date:~0,10%" echo exp 用户名/密码 ...
- ssh框架文件上传下载
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- SpringMVC 表单标签 & 处理静态资源
使用 Spring 的表单标签 通过 SpringMVC 的表单标签可以实现将模型数据中的属性和 HTML 表单元素相绑定,以实现表单数据更便捷编辑和表单值的回显. form 标签 一般情况下,通过 ...
- centos下在线安装mysql
1 首先查看是否有安装过,如果已经安装过,就不必再安装了 yum list installed mysql* rpm -qa | grep mysql* 2 查看有没有安装包: yum list my ...
- ICML历年Best Papers
作者:我爱机器学习原文链接:ICML历年Best Papers ICML (Machine Learning)(1999-2016) 2016 Dueling Network Architecture ...
- UIButtonTypeSystem backBarButtonItem
当UIButton是UIButtonTypeSystem类型时,改变UIButton的frame,系统会有一个动画改变效果,不想要这个效果,将类型改为UIButtonTypeCustom. backB ...