COM Component Development Practice (V)---from C + + to COM:P Art 2

Source: Internet
Author: User
Tags abstract

One, reusing C + + objects using abstract base classes

In the previous article, COM Component Development Practice (IV)---from C + + to COM:P Art 1, we've encapsulated the C + + object that we're reusing into a DLL, and the Declaration and implementation of the object has been stripped down, But there is a problem: the private members of the object (such as the array variable m_arrtables of the CDB class in our example) are clearly visible to customers, even if the client is not able to access them; If the object changes the size of its data member, all client programs must be recompiled.

In fact, the customer needs only the address of the member function of the object, so using the abstract base class can satisfy the above requirements, the customer will not include the object's private data members, even if the object changes the size of the data member, the client program does not have to recompile.

1. Modify interface file

First, you change the interface to an abstract base class, which is the only code required by the client program. Including the following steps: 1 The functions of CDB and cdbsrvfactory are changed into pure virtual functions. 2 Delete data members. 3 Delete all the member functions of the derivation of the flag. 4 Change the CDB to IDB (the interface of DB), Cdbsrvfactory to Idbsrvfactory (the interface of DB class factory)

typedef long HRESULT;
#define Def_export __declspec (dllexport)
Class IDB
{
Interfaces
Public
Interface for data access
Virtual HRESULT Read (short ntable, short nrow, lpwstr lpszdata) = 0;
Virtual HRESULT Write (short ntable, short nrow, lpcwstr lpszdata) = 0;
Interface for database management
Virtual HRESULT Create (short &ntable, lpcwstr lpszname) = 0;
Virtual HRESULT Delete (short ntable) = 0;
Interfase para obtenber informacion sobre la base de datos
Virtual HRESULT getnumtables (short &nnumtables) = 0;
Virtual HRESULT gettablename (short ntable, lpwstr lpszname) = 0;
Virtual HRESULT getnumrows (short ntable, short &nrows) = 0;
Virtual ULONG release () = 0;
};
Class Idbsrvfactory
{
Interface
Public
Virtual HRESULT createdb (idb** ppobject) = 0;
Virtual ULONG release () = 0;
};
  
HRESULT def_export dllgetclassfactoryobject (idbsrvfactory * * ppobject);

2. Modify Object Program

In the DLL project, we implement the Declaration and implementation of the specific subclass for this abstract interface, let CDB derive from IDB, cdbsrvfactory derive from Idbsrvfactory, and the parameters of the Createdb method of the class factory cdbsrvfactory by cdb** Changed into idb**.

#include ". \interface\dbsrv.h "
typedef long HRESULT;
Class Cdb:public IDB
{
Interfaces
Public
Interface for data access
HRESULT Read (short ntable, short nrow, LPWStr lpszdata);
HRESULT Write (short ntable, short nrow, lpcwstr lpszdata);
Interface for database management
HRESULT Create (short &ntable, lpcwstr lpszname);
HRESULT Delete (short ntable);
Interfase para obtenber informacion sobre la base de datos
HRESULT getnumtables (short &nnumtables);
HRESULT Gettablename (short ntable, LPWStr lpszname);
HRESULT getnumrows (short ntable, short &nrows);
ULONG release (); Cpptocom:need to free an object in the DLL, since it is allocated here
Implementation
Private
CPtrArray M_arrtables; Array of pointers to CStringArray (the "database")
CStringArray M_arrnames; Array of table names
Public
~CDB ();
};
Class Cdbsrvfactory:public Idbsrvfactory
{
Interface
Public
HRESULT createdb (idb** ppobject);
ULONG release ();
};

The implementation code for these two specific subclasses is omitted from this list, referring to the previous article.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.