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

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的更多相关文章

  1. Clean Code – Chapter 6 Objects and Data Structures

    Data Abstraction Hiding implementation Data/Object Anti-Symmetry Objects hide their data behind abst ...

  2. 20182320《Program Design and Data Structures》Learning Summary Week9

    20182320<Program Design and Data Structures>Learning Summary Week9 1.Summary of Textbook's Con ...

  3. [轉]Linux Data Structures

    Table of Contents, Show Frames, No Frames Chapter 15 Linux Data Structures This appendix lists the m ...

  4. Persistent Data Structures

    原文链接:http://www.codeproject.com/Articles/9680/Persistent-Data-Structures Introduction When you hear ...

  5. Choose Concurrency-Friendly Data Structures

    What is a high-performance data structure? To answer that question, we're used to applying normal co ...

  6. Objects and Data Structures

    Date Abstraction Hiding implementation is not just a matter of putting a layer of fucntions between ...

  7. Persistent and Transient Data Structures in Clojure

    此文已由作者张佃鹏授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 最近在项目中用到了Transient数据结构,使用该数据结构对程序执行效率会有一定的提高.刚刚接触Trans ...

  8. 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 ...

  9. Important Abstractions and Data Structures

    For Developers‎ > ‎Coding Style‎ > ‎ Important Abstractions and Data Structures 目录 1 TaskRunne ...

随机推荐

  1. java selenium (四) 使用浏览器调试工具

    在基于UI元素的自动化测试中, 无论是桌面的UI自动化测试,还是Web的UI自动化测试.   首先我们需要查找和识别UI元素. 在基于Web UI 自动化测试中, 测试人员需要了解HTML, CSS和 ...

  2. 不一样的Android选择器,简单方便,地址日期时间都好用!

    前言 Android开发有不少情况下会用到Picker,例如选择 地址.日期.时间. 原生Picker和仿iOS的Picker都是上下或左右滑动到固定区域来选择选项: 显示数量少,如果当前选项距离需要 ...

  3. 美化iTerm2

    一.下载iTerm2,启动 二.安装oh-my-zsh curl -L https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/insta ...

  4. PHP初步(上)

    一.php语法 PHP是一种嵌入式脚本语言,所以,我们需要标记出哪些是PHP代码,哪些是html代码.当html和php代码进行混编的时候,文件必须要以php结尾(否则Apache不会将这个文件交给p ...

  5. #1000 A + B (hihoCoder)

    时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 求两个整数A+B的和 输入 输入包含多组数据.每组数据包含两个整数A(1 ≤ A ≤ 100)和B(1 ≤ A ≤ 100) ...

  6. 超简单的js数字验证

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or ...

  7. shared_ptr和多线程

    前一篇文章写得实在太挫,重新来一篇. 多线程环境下生命周期的管理 多线程环境下,跨线程对象的生命周期管理会有什么挑战?我们拿生产者消费者模型来讨论这个问题. 实现一个简单的用于生产者消费者模型的队列 ...

  8. Cairo 下载,测试

    You need to download the all-in-one bundle available here. You can discover this link yourself by vi ...

  9. ajax+php数据增加查询获取删除

    前段代码部分其实前面已经有写出但是做一点修改所以还是贴出来,可能大家看到代码回不理解,看完图我想大家会理解我为什么这么写了,这和前端布局有关系的,先列出内容在选择内容删除或修改 <!DOCTYP ...

  10. ViewPager实现自动翻页功能 --转载出处找不到了,根据自己的理解写个随笔方便以后的记忆以及代码的共享,感谢给我启发的那位高手--第一次写博客哈

    xml文件 textview 用于显示图片的标题 viewpager 用于实现翻页效果 <LinearLayout xmlns:android="http://schemas.andr ...