Search code examples
c++dlloperator-overloadingdeclspec

Do overloaded operators within classes require __declspec(dllexport) if they're used in a DLL project?


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?


Solution

  • 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.