私は0122,を介して、C++のdll境界について読んでいます。C++のDllの境界は、抽象的なインターフェイスを使用して - ヘッダーの>スマートポインタ?削除を呼び出す?
私は単一のプロジェクトで実装例を書いた。
main.cpp
#include <iostream>
#include <memory>
#include <functional>
#include "Interface.hpp"
typedef std::shared_ptr<Interface> IPTR;
int main()
{
IPTR ptr = std::shared_ptr<Interface>(MakeInterface(), std::mem_fn(&Interface::Release));
std::cout << ptr->Foo(4) << "\n";
}
Interface.hpp
#define MyAPI /*export/import*/
#define APIENTRY __stdcall
struct MyAPI Interface
{
virtual int Foo(int n) = 0;
virtual void Release() = 0;
};
extern "C" MyAPI Interface* APIENTRY MakeInterface();
Interface.cpp
#include "Interface.hpp"
struct InterfaceImpl : Interface
{
virtual int Foo(int n){ return n; }
virtual void Release(){ delete this; }
};
extern "C" MyAPI Interface* APIENTRY MakeInterface()
{
return new InterfaceImpl{};
}
私の質問は
-
です
- スマートポインタ宣言をC++のdllの問題に陥らずにヘッダーに移動できますか?
- Releaseメソッドで
delete this
に電話するのは正しいですか?
技術的には、「dllの境界はC++で」(まだ)存在しません。なぜCOMを書き直したいのですか? –
@ダン私はあなたの意見を見て、瞬時に私が何を得ているかを知っていました:) – Johannes
しかし、あまりにも多くの貧弱なC++の質問を読んだ後は、よく尋ねられたものを見てうれしいです... :-) * Sei gegruesst!* –