gpt4 book ai didi

c++ - pImpl 是否从根本上解决了 C++ DLL 问题?

转载 作者:太空狗 更新时间:2023-10-29 23:14:51 28 4
gpt4 key购买 nike

我正在尝试从具有 STL 成员的 DLL 中导出 C++ 类。

这是我的主课。

class MATHFUNCSDLL_API MyMathFuncsImpl
{
public:

std::vector<int> vi;
std::string getString();
void setString(std::string s);
private:
std::string s;
};

使用这些方法有效,但在 VS 2012 上给出关于 std::string 和 std::vector 没有 dll 接口(interface)的警告。现在,当我这样做时 -

class  MATHFUNCSDLL_API MyMathFuncs
{
public:
MyMathFuncs()
{
pImpl = new MyMathFuncsImpl();
}
std::string getString()
{
return pImpl->getString();
}

std::vector<int> getVector()
{
return pImpl->vi;
}

void setString(std::string news)
{
pImpl->setString(news);
}
private:
MyMathFuncsImpl* pImpl;
};

我没有收到任何警告,而且它也有效。我的问题是:拥有这样的接口(interface)真的能解决问题(STL 成员可能在 dll 边界上以不同方式实现),还是它只是抑制编译器问题的一个技巧?

最佳答案

是的。

永远不要在 DLL 接口(interface)中使用内联函数。这包括应该显式声明但在实现文件中定义的构造函数和析构函数。对于 C++,这意味着您应该使用 pImpl 惯用法或虚拟基类。

避免内联函数的原因是它们可能会从一个版本更改到下一个版本,而 DLL 保持不变。任何不匹配都会导致奇怪和可怕的问题。

关于c++ - pImpl 是否从根本上解决了 C++ DLL 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31755215/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com