简体   繁体   English

导出类中的std :: Vector成员

[英]Export std::Vector Member in a Class

I want to export a C++ class, which has a member std::vector<int> . 我想导出一个C ++类,该类具有成员std::vector<int> How to export this class so that my C# application can consume it? 如何导出此类,以便我的C#应用​​程序可以使用它? And how to write the corresponding .Net code? 以及如何编写相应的.Net代码?

This is what I tried so far, following the examples given here . 到目前为止,我已经按照此处给出的示例进行了尝试。

 #include <vector>
#  define BOOSTGRAPH_API __declspec(dllexport)
# define EXPIMP_TEMPLATE

EXPIMP_TEMPLATE template class BOOSTGRAPH_API std::vector<int>;

     class BOOSTGRAPH_API MyClass
    {
    public:
        std::vector<int> VectorOfInts;

    public:
        bool operator < (const MyClass  c) const
        {
            return VectorOfInts < c. VectorOfInts;
        }
        bool operator == (const MyClass  c) const
        {
            return VectorOfInts == c. VectorOfInts;
        }
    };

But then I'm stucked. 但是后来我被困住了。

C# can't understand __declspec(dllimport), it can only grok C-style interfaces. C#无法理解__declspec(dllimport),它只能显示C风格的接口。 You will have to go through C++/CLI, they offer STL->.NET conversions, IIRC. 您将必须通过C ++ / CLI,它们提供STL->。NET转换,即IIRC。

What are you trying to do? 你想做什么? I see BOOST_GRAPH_API, what are you up to? 我看到了BOOST_GRAPH_API,您在做什么? You will not be able to use native C++ code from .Net directly, neither you should! 您将不能直接使用.Net的本机C ++代码,也不能! That would force you to compile all imported dependencies on C++ standard library and boost(!) with clr support... 这将迫使您编译所有导入的对C ++标准库的依赖并通过clr支持对boost(!)进行编译...

You need to define interfaces to your classes along with proper factory methods. 您需要定义类的接口以及正确的工厂方法。 These can be safely exported and wrapped by C++/CLI objects which you can directly reference within .Net programs: 这些可以通过C ++ / CLI对象安全地导出和包装,您可以在.Net程序中直接引用这些对象:

// imyclass.h

__declspec( dllexport ) 
IMyClass* CreateInstance();

__declspec( dllexport ) 
class IMyClass {
public:
    virtual int CompareTo(IMyClass*) = 0;
    virtual ~IMyClass() {}
};

// myclass.h

#include "imyclass.h"
#include <vector>

class MyClass : public IMyClass {
    std::vector<int> mIntVector;
public:
    virtual int CompareTo(IMyClass*);
    // constructors and access functions go here...
};

// myclass.cpp

#include "myclass.h"
#include <memory>

IMyClass* CreateInstance() {
 return new MyClass;
}

// implementations of whatever MyClass should do go here...

Observe that only the interface IMyClass and its creator is exported by the dll, nothing else. 请注意,dll仅导出了接口IMyClass及其创建者,其他都没有。 In a C++/CLI project, you define a C++/CLI disposable wrapper which holds a pointer to IMyClass and imports only imyclass.h. 在C ++ / CLI项目中,您定义一个C ++ / CLI一次性包装器,该包装器包含指向IMyClass的指针,并且仅导入imyclass.h。 The wrapper which imprt only IMyClass and it's creator should be similar to something shown below, but take care to get the finalizer semantics right, not sure at the moment what is generated autmatically by the compiler...: 仅封装IMyClass及其创建者的包装器应类似于以下所示,但要注意正确确定终结器的语义,目前尚不确定编译器自动生成的内容...:

ref class IMyClassWrapper {
    IMyClass* mPtrResource;

 public:

    int CompareTo(IMyClassWrapper^ pOther) {
        return this->mPtrResource->CompareTo(pOther->mPtrResource);
    }

    IMyClassWrapper() {
     mPtrResource = ::CreateInstance();
    }

    ~IMyClassWrapper() {
     this->!IMyClassWrapper();
    }

 protected:

    !IMyClassWrapper() {
     delete mPtrResource;
     mPtrResource = 0;
    }

};

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

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