简体   繁体   English

C ++从指针构造

[英]C++ Construct from pointer

I am trying to make my project complied with GCC to link with library compiled with MSVC (that I have no sources for). 我试图使我的项目符合GCC,以链接使用MSVC(我没有资料)编译的库。 The problem is that this library also exists for linux x86, x86_64 and arm platforms compiled with GCC (and my project is crossplatform) and I do not want to write a lot of platform-dependent (and compiler-dependent) code. 问题在于该库也存在于使用GCC编译的linux x86,x86_64和arm平台上(我的项目是跨平台的),我不想编写很多依赖于平台(且依赖于编译器)的代码。 Also other libraries linked to project are designed to be compiled by GCC (like ffmpeg and vlc) and I have no possibility to migrate to MSVC. 另外,链接到项目的其他库也被设计为由GCC编译(例如ffmpeg和vlc),我无法移植到MSVC。 For that I've written C-style wrapper library (based on original headers). 为此,我编写了C样式的包装器库(基于原始标头)。

Constructors and destructors for classes are done this way: 类的构造函数和析构函数是通过以下方式完成的:

__declspec(dllexport) void* myClass_myClass()
{
    void* ptr = new myClass();
    return ptr;
}
__declspec(dllexport) void myClass_unmyClass(void* ptr)
{
    delete((myClass*)ptr);
}

This part is compiled with MSVC and linked to original library. 此部分使用MSVC编译,并链接到原始库。 As you see (and I can see it in dependency walker) I have c-style exports that I can link with GCC. 如您所见(我可以在dependency walker中看到它),我有可以与GCC链接的c样式导出。 It allocates memory, creates object and acts as designed. 它分配内存,创建对象并按设计进行操作。

Now I am writing second part of this wrapper that will contain classes and will be compiled with GCC and linked to my project. 现在,我正在编写此包装的第二部分,其中将包含类,并将使用GCC进行编译并链接到我的项目。 It should be based on original headers of library (to make sure it acts the same as original with both linux- and windows- versions of this library). 它应基于库的原始头文件(以确保该库的作用与该库的linux和Windows版本的原始头相同)。 And I am in doubts how to make constructor and destructor of classes. 而且我对如何制作类的构造函数和析构函数感到怀疑。 Let's say we have 假设我们有

class MyClass
{
public:
    MyClass()
    ~MyClass()
}

in original header. 在原始标题中。

How it can be defined in .cpp to bypass call to my c-style "consturctor" and "destructor" (so they will bypass calls to original constructors and destructors)? 如何在.cpp中定义它以绕过对我的c样式“构造函数”和“析构函数”的调用(因此它们将绕过对原始构造函数和析构函数的调用)? It would be great to assign pointer to this in constructor, but it is forbidden. 在构造函数中分配指向this指针会很棒,但是这是禁止的。

PS I have no internals of classes. PS我没有类的内部。 They all have 他们都有

private: myClassImpl* m_pImpl;

so I can't use copy constructor. 所以我不能使用拷贝构造函数。 Even if I could - I do not want to construct 2 times the same object. 即使我可以-我也不想构造相同对象的2倍。

Your wrapper class should be a handle class which calls myClass_myClass() in the constructor, stores the returned pointer as a member, and calls myClass_unmyClass() in the destructor on the stored pointer. 包装器类应该是一个句柄类,它在构造函数中调用myClass_myClass() ,将返回的指针存储为成员,并在析构函数中对存储的指针调用myClass_unmyClass() All member functions in the original class should be copied in the wrapper, but just call the original functions (via wrapper "C" functions) on the stored pointer. 原始类中的所有成员函数都应复制到包装器中,而只需在存储的指针上调用原始函数(通过包装器“ C”函数)。 Be careful with copy-constructors and copy-assignment operators -- the defaults won't do the right thing. 注意复制构造函数和复制分配运算符-默认值不会做正确的事情。 You must either delete them or implement them to call appropriate wrapper "C" functions. 您必须删除它们或实现它们才能调用适当的包装器“ C”函数。

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

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