Click here to Skip to main content
15,891,851 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Now I dev a new libary based on another free-libary.but I also don't want the people knows My libary 's Imp details.
EX.
a database class in the free lib: OdDatabse.
I want to Imp my own class(MyDatabase) which is same with OdDatabse.

I really don't How to Do this.

I hava tried this:

.h file:

C++
class MyDatabase;//declare

void Dosth(MyDatabase* pDb);//A function used MyDatabase


.cpp file.
C++
#include "OdDatabse.h"  //For hiding imp details ,The free-lib info,cann't be in .h file,because .h file will offer to user

typedef OdDatabse MyDatabase;// This cann't pass,the compiler will alter!


Some body can help me ? or some better advice,As simple as possible ,Thanks in advance!
Posted
Comments
Sergey Alexandrovich Kryukov 9-Apr-13 23:40pm    
Is it English or what? I can understand almost each word, but I don't understand most combination. I don't believe you can get an answer. Could you do anything about that? Also, it looks like the post is based on some misconceptions, but I cannot figure out which...
—SA
Richard MacCutchan 10-Apr-13 3:47am    
You cannot hide it completely, and why would you want to, unless you are doing something illegal.

There are basically two ways to provide your own interface for a third party class.

1. Inheritance:
This wouldn't work, as you need to include the header file in your own header file, and you wish to avoid this.

2. Using delegates:
Your own class creates an instance of the library class on the heap and only stores its pointer. Here's a simple example:

C++
// library foobar
// header foo.h
class foo {
public:
   void doSomething();
   void doSomeOtherThing();
};

// library foobar
// header bar.h
#include "foo.h" 
class bar {
public:
   void process(foo* f);
};


Your delegate class declarations:
C++
// header myfoo.h
class foo; // forward declaration;
class myfoo {
   foo* foo_delegate;   // only a pointer, it does not need foo.h
   friend class mybar;  // see below for explanation
public:
   myfoo();             // constructor is required, see implementation
   ~myfoo();            // destructor required as well
   void doSomething();
   void doSomeOtherThing();
};

// header mybar.h
class bar;
#include "myfoo.h"
class mybar {
   bar* bar_delegate;
public:
   mybar();
   ~mybar();
   void process(myfoo* mf);
};

And your delegate implementation:
C++
// implementation file myfoo.cpp
#include "myfoo.h"
#include "foo.h"
myfoo::myfoo() {
   foo_delegate = new foo; // this pointer must be initialized in every constructor
}
myfoo::~myfoo() {
   delete foo_delegate;    // clean up the delegate
}
void myfoo::doSomething() {
   foo_delegate->doSomething();
}
void myfoo::doSomeOtherThing() {
   foo_delegate->doSomeOtherThing();
}

// implementation file mybar.cpp
#include "mybar.h"
#include "bar.h"
mybar::mybar() {
   bar_delegate = new bar;
}
mybar::~mybar() {
   delete bar_delegate;
}
void mybar::process(myfoo* mf) {
   foo* f = mf->foo_delegate; // private member requires friend declaration
   bar_delegate->process(f); 
}

Now you can use your class delegates myfoo and mybar exactly like you would use the original classes foo and bar. And you have the option to change any aspect of this interface, within your own implementation. The only trace of the original classes are the delegate pointers which do not require including the corresponding headers in your own header files.

PS: the method mybar::process shows how you can delegate any function that expects argument types of the hidden library. You need to be careful that your delegates may need access to the delegate pointers inside other classes, that is the reason for the friend declaration in class myfoo.
 
Share this answer
 
v2
Solution #1 gives 2 methods. A third mechanism you can use is creation of a class within a namespace and interface to the target class in the global namespace (or vice versa).

Studying the basic information for namespaces should give you all the info you need on how to do this.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900