如果类中的重载运算符在 DLL 项目中使用,是否需要 __declspec(dllexport)?
使用 __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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您通常会将 __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.
像这样的函数通常会被内联。我无法想象为什么您想要强制执行跨模块(间接和修复)函数调用。
但我建议不要导出类,因为它会在两个 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.