如何与另一个同名? [英] How can I make a same class name with another?

查看:57
本文介绍了如何与另一个同名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我开发了一个基于另一个免费图书馆的新图书馆。但我也不希望人们知道我的图书馆的Imp细节。

EX。

a数据库类在free lib中:OdDatabse。

我想要Imp我自己的类(MyDatabase)和OdDatabse一样。



我真的不怎么做这个。



我hava试过这个:



.h文件:



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:

class MyDatabase;//declare

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





.cpp文件。



.cpp file.

#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!

推荐答案

基本上有两种方法可以为第三方类提供自己的界面。



1.继承:

这不行,因为您需要在自己的头文件中包含头文件,并且您希望避免这种情况。



2.使用委托:

你自己的类在堆上创建一个库类的实例,只存储它的指针。这是一个简单的例子:



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:

// 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:

// 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:

// 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); 
}



现在你可以使用你的班级代表 myfoo mybar 与使用原始类 foo bar 完全一样。您可以选择在自己的实现中更改此接口的任何方面。原始类的唯一痕迹是委托指针,它们不需要在您自己的头文件中包含相应的标题。



PS:方法 mybar :: process 显示了如何委托任何需要隐藏库的参数类型的函数。您需要注意您的委托可能需要访问其他类中的委托指针,这就是类 myfoo <中 friend 声明的原因/ code>。


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.


解决方案#1提供了2种方法。您可以使用的第三种机制是在命名空间中创建一个类,并在全局命名空间中创建目标类的接口(反之亦然)。



学习基本信息对于名称空间应该为您提供有关如何执行此操作所需的所有信息。
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.


这篇关于如何与另一个同名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆