In the previous approach we realized that the main issue with with C++ is that encapsulation does not encapsulate at binary level.This would mean that same class cannot be interface and implement it. The solution is to have two abstractions as two distinct entities using two C++ classes.
One C++ class has to be developed only to expose the functionality and the other to implement it. In this way we can ensure that we can change the implementation behind the scene and ship to the customers.
the following is the implementation of FastStringIft class for encapsulating implementation class FastString.
//faststringitf.cpp // (part of DLL, not client)
One C++ class has to be developed only to expose the functionality and the other to implement it. In this way we can ensure that we can change the implementation behind the scene and ship to the customers.
the following is the implementation of FastStringIft class for encapsulating implementation class FastString.
//faststringitf.h
class __declspec(dllexport) FastStringltf {class FastString; // introduce name of impl. classpublic:
FastString *m_pThis; // opaque pointer
// (size remains constant)
FastStringltf(const char *psz);} ;
-FastStringltf(void);
int Length(void) const; // returns # of characters
int Find(const char *psz) const; // returns offset
//faststringitf.cpp // (part of DLL, not client)
#include "faststring.h"
#include "faststringitf.h"
FastStringltf::FastStringltf(const char *psz)
m_pThis(new FastString(psz)) {
assert(m_pThis != 0);
}
FastStringltf: :-FastStringltf(void) {
delete m_pThis;
}
int FastStringltf::Lengthltf(void) const {
return m_pThis->Length();
}
int FastStringltf::Find(const char *psz) const {
return m_pThis->Find(psz);
}
Now consider the previous problem, if the size of the of FastString class size changes from 4 Bytes to 8 Bytes . When we compile the DLL the FastString will be instantiated in the DLL itself ( new FastString(psz) in FastStringIft.cpp ) allocating the required size to instantiate the FastString object. Now, when client calls the FastStringItf , the solution will work correctly because there is no impact on definition of the interface class.
No comments:
Post a Comment