2

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?

Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
Darkenor
  • 4,349
  • 8
  • 40
  • 67

2 Answers2

4

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thanks. The Microsoft walkthrough on creating a DLL is pretty crappy. Glad someone on here is a little more helpful! :) – Darkenor Mar 25 '11 at 18:50
3

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.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720