简体   繁体   中英

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. I'm using the #import mechanism to work with Excel's COM, so right now I have:

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

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.

So, is there any way to forward declare _com_ptr_t pointers like Excel::_ApplicationPtr? 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. Have all the functions be virtual. (Yes, this is basically creating an "interface". (Call it CExcelApp ...)

Derive a class from the first. In it you add the m_app variable and override all your virtual functions. You don't expose this class to your users. (Call it CExcelAppImp ... or whatever)

In your base class make a static function that instantiates an instance of CExcelApp, but in the implementation, it would instantiate 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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