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

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. $(this)在ajax中无效的解决方案

    在ajax方法里写$(this)指向的是最近调用它的jquery对象,所以这里的$(this)指的是ajax对象,而不是$(".enter_caozuo").find(" ...

  2. 在Heroku上,安装Wordpress

    其實在 Heroku 上安裝 Wordpress 不會很難,不過閱讀之前,你可能先要知道 Heroku 與 git 的基本操作,建議可以先參考以下網站用 Heroku 架設 Wordpress 網站 ...

  3. 6、HTML5表单提交和PHP环境搭建

    ---恢复内容开始--- 1.块元素 块元素在显示的时候,通常会以新行开始 如:<h1> <p> <ul> <!-- 块—>注释 <p>he ...

  4. gtp转换mbr

    http://wenku.baidu.com/link?url=P_t0U8Q-LIUdxVGHBefipAvbV6fg3jnX8hc8ugaRoo5WWd8GJePO8sBbtLON15gvOZh4 ...

  5. MSP430FR5739串口程序

    今天急着用这个片子的串口,匆忙中调试串口也话费了一段时间,在网上下了一个程序,忽然就把所有问题搞清楚了,只是中断就看着头文件中寄存器写的,虽然通讯正常,不过不确定有没有写错.代码如下: #includ ...

  6. arrayLen

    var i;for (i = categoryList.length - 1; i >= 0; i -= 1) { var categoryValue=categoryList[i]; if ( ...

  7. JS中函数的调用和this的值

    调用每一个函数会暂停当前函数的执行,传递控制权和参数给新函数.除了声明时定义的形式参数,每个函数还接收两个附加的参数:this 和 arguments. 参数this在面向对象编程中非常重要,他的值取 ...

  8. N皇后问题-Hdu 2553

      题目描述: 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上. 你的任务是,对于给定的N,求出有多少种合法的放置 ...

  9. Qt之C语言类型typedef a[]等

    01:typedef类型:给类型起别名,typedef int d[5];定义了一个类型即一个5个int类型的数据.所以d c;的长度是就是4 * 10:

  10. jQuery性能优化

    1. 优化选择器执行的速度 优先使用ID与标记选择器 在jQuery中,访问DOM元素的最快方式是通过元素ID号,其次是通过元素的标记.因为前者源于JavaScript中的document.getEl ...