如果类中的重载运算符在 DLL 项目中使用,是否需要 __declspec(dllexport)?

发布于 2024-10-27 14:41:06 字数 406 浏览 1 评论 0原文

使用 __declspec(dllexport) 时,重载运算符是否也应该分配此导出标志?例如,假设您有类似的内容:

/* 
Overloaded operator (equivalent operator) returns true if x equals compared vector
*/
__declspec(dllexport) bool operator ==(const vector &v)
{
  return (x == v.x && y==v.y && z==v.z);
}

为了在您的类类型上使用 == ,是否需要 __declspec(dllexport) ?或者不应该导出它,因为它特定于该类和任何继承的类?

When using __declspec(dllexport), should overloaded operators also have this exportation flag assigned? For example, say you have something like:

/* 
Overloaded operator (equivalent operator) returns true if x equals compared vector
*/
__declspec(dllexport) bool operator ==(const vector &v)
{
  return (x == v.x && y==v.y && z==v.z);
}

Is the __declspec(dllexport) necessary in order to use == on your class type? Or should that not be exported because it's specific to that class and any inherited classes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟燃烟灭 2024-11-03 14:41:06

您通常会将 __declspec(dllexport) 应用于类声明,以便导出整个 shebang。还导出 v 表,如果类有虚拟成员,这一点很重要。一个人同时做这件事是相当累人和麻烦的。

不知道为什么你要跳过超载。如果您在类中将其公开,那么您肯定也应该从 DLL 中公开它。如果你不这样做,那么有一天有人将很难诊断链接器错误。

You'd normally apply __declspec(dllexport) to the class declaration so the whole shebang gets exported. Also exports the v-table, important if the class has virtual members. Doing it one member at the time is pretty tiresome and troublesome.

No real idea why you'd skip the overload. If you made it public in the class then you definitely ought to expose it from the DLL as well. If you don't then somebody is going to have a very hard time diagnosing the linker error some day.

温柔戏命师 2024-11-03 14:41:06

像这样的函数通常会被内联。我无法想象为什么您想要强制执行跨模块(间接和修复)函数调用。

但我建议不要导出类,因为它会在两个 DLL 之间造成紧密耦合,这在以后的维护中会带来真正的麻烦。

A function like that is normally going to be inlined. I can't imagine why you would want to force a cross-module (indirection and fix-ups) function call instead.

But I counsel against exporting classes, since it creates tight coupling between the two DLLs, which is a real maintenance headache later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文