, / C++. , , . , - , ,
- , / - , C++ DLL .
, , C++ , , / . , , . , , - (structs), . , , , (pragmas) . , , ( , ) . , , . WINAPI/WINBASEAPI Win32 API. DLL :
WINBASEAPI void WINAPI Sleep(DWORD dwMsecs);
. , .
, : C++ . , , , , . :
class calculator { public: virtual void add1(short x); virtual void add2(short x, short y); };
:
extern calculator *pcalc; pcalc->add1(1); pcalc->add2(1, 2);
, , . , , .
, , . C++ vptr vtbl . , . vtbl (virtual function table ) , . , vptr (virtual function pointer - ); vtbl . , , vptr, vtbl , . C++ . 1.5 vptr/vtbl calculator, .
C++ vprt vtbl. vtbl: CFRONT (adjuster thunk). . , ( Win32 adjuster thunk, Solaris CFRONT vtbl). vtbl C++, , . " C++ " (Stan Lippman. Inside C++ Object Model).
, . , , C++ , ; , . , . , .
, , , , .
// ifaststring.h class IFastString { public: virtual int Length(void) const = 0; virtual int Find(const char *psz) const = 0; };
, . vtbl , (null), - (_purecall Microsoft C++), . , vtbl , . . . y . , ( vptr/vtbl). , " " ("is-a") C++ , - :
class FastString : public IFastString { const int m_cch; // count of characters // char *m_psz; public: FastString(const char *psz); ~FastString(void); int Length(void) const; // returns # of characters // int Find(const char *psz) const; // returns offset // };
FastString IFastString, FastString IFastString. , FastString vptr, vtbl IFastString. FastString , vtbl Length Find. . 1.6.
, , FastString, . ; . FastString DLL , . , extern " ", C++.
// ifaststring.h class IFastString { public: virtual int Length(void) const = 0; virtual int Find(const char *psz) const = 0; }; extern "C" IFastString *CreateFastString(const char *psz); // faststring.cpp (part of DLL) // faststring.cpp ( DLL) IFastString *CreateFastString (const char *psz) { return new FastString(psz); }
- , DLL FastString, , , .
, , . , :
int f(void) { IFastString *pfs = CreateFastString("Deface me"); int n = pfs->Find("ace me"); delete pfs; return n; }
, . , delete . FastString , "Deface me", .
. , , vtbl . Delete , . . :
// ifaststring.h class IFastString { public: virtual void Delete(void) = 0; virtual int Length(void) const = 0; virtual int Find(const char *psz) const = 0; }; extern "C" IFastString *CreateFastString (const char *psz);
:
// faststring.h #include "ifaststring.h" class FastString : public IFastString { const int m_cch; // count of characters // char *m_psz; public: FastString(const char *psz); ~FastString(void); void Delete(void); // deletes this instance // int Length(void) const; // returns # of characters // int Find(const char *psz) const; // returns offset // }; // faststring.cpp #include <string.h> #include "faststring.h" IFastString* CreateFastString (const char *psz) { return new FastString(psz); } FastString::FastString(const char *psz) : m_cch(strlen(psz)), m_psz(new char[m_cch + 1]) { strcpy(m_psz, psz); } void FastString::Delete(void) { delete this; } FastString::~FastString(void) { delete[] m_psz; } int FastString::Lengtn(void) const { return m_cch; } int FastString::Find(const char *psz) const { // O(1) lookup code deleted for clarity // 0(1) }
1.7 FastString . FastString, CreateFastString:
#include "ifaststring.h" int f(void) { int n = -1; IFastString *pfs = CreateFastString("Hi Bob!"); if (pfs) { n = pfs->Find("ob"); pfs->Delete(); } return n; }
, , , DLL FastString . , , vtbl, . , . , , CreateFastString , FastString. , , extern " ", . , C++ , DLL . , DLL, C++, C++. .