简体   繁体   English

如何向前声明_com_ptr_t指针?

[英]How to forward-declare _com_ptr_t pointers?

I want to create a library to wrap Excel Automation and expose just some of its vast functionality. 我想创建一个用于包装Excel Automation的库,并仅公开其某些功能。 I'm using the #import mechanism to work with Excel's COM, so right now I have: 我正在使用#import机制与Excel的COM配合使用,所以现在我有了:

// EXCELAPP.H
#import "C:\\PathTo\\mso.dll" //...
#import "C:\\PathTo\\VBE6EXT.OLB" //...
#import "C:\\PathTo\\EXCEL.EXE" //...

class ExcelApp
{
public:
    ExcelApp();
    ~ExcelApp();

    void CloseExcel();

    void ShowWindow();
    void HideWindow();
    // ...

private:
    Excel::_ApplicationPtr m_app;
};

This is in a static library project, and I'm using it just fine from my program. 这是在静态库项目中,我在程序中使用它就很好。

The thing is, I would like to "hide" from the users of the library how it is implemented. 问题是,我想对库用户“隐藏”它是如何实现的。 The implementation could change in the future. 将来可能会更改实现。 Also, having the imports in the .h file exposes all the COM interface to the users of the library and I don't want them (my future-self included) to abuse it. 另外,在.h文件中导入文件会将所有COM接口暴露给库的用户,我不希望它们(包括我的未来自我)滥用它。

So I thought of doing something like the PImpl idiom, but I would need to at least forward-declare m_app, and I have no idea how to do that. 所以我想做类似PImpl习惯用法的事情,但是我至少需要向前声明m_app,而且我不知道该怎么做。

So, is there any way to forward declare _com_ptr_t pointers like Excel::_ApplicationPtr? 因此,有什么方法可以像Excel :: _ ApplicationPtr一样转发声明_com_ptr_t指针? Or is there a better way to do what I'm trying to do? 还是有更好的方法来做我想做的事情?

If you really want to do it that way. 如果您真的想那样做。 I would create a baseclass that does not have the m_app pointer. 我将创建一个没有m_app指针的基类。 Have all the functions be virtual. 将所有功能虚拟化。 (Yes, this is basically creating an "interface". (Call it CExcelApp ...) (是的,这基本上是在创建一个“接口”。(将其称为CExcelApp ...)

Derive a class from the first. 从第一课开始。 In it you add the m_app variable and override all your virtual functions. 在其中添加m_app变量并覆盖所有虚拟函数。 You don't expose this class to your users. 您不会向用户公开此类。 (Call it CExcelAppImp ... or whatever) (将其称为CExcelAppImp ...或其他名称)

In your base class make a static function that instantiates an instance of CExcelApp, but in the implementation, it would instantiate CExcelAppImp. 在您的基类中,创建一个静态函数来实例化CExcelApp的实例,但是在实现中,它将实例化CExcelAppImp。

class CExcelApp
{
protected:
   CExcelApp(); // make your constructors protected

public:
static CExcelApp* CreateInstance();
};

// in your implementation

CExcelApp CExcelApp::CreateInstance()
{
   return new CExcelAppImp();
}

This is just one way... 这只是一种方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM