Not necessary to export class with only virtual/inline functions?

后端 未结 4 1109
鱼传尺愫
鱼传尺愫 2021-01-19 02:14

In C++ on Win32:

Suppose I have a DLL with a header file that declares a class. The DLL exports some means of obtaining a pointer/reference to an instance of that c

相关标签:
4条回答
  • 2021-01-19 02:26

    Am I correct in believing ...

    Yes I think so, but:

    • You should test that (I can't at the moment)

    • You might want to beware using inline methods: because if you change them later then you ought to rebuild every other component which depends on (has been built with) this class (i.e. the DLL is no longer at all well-insulated or self-contained ... inline methods are OK within a single DLL, but conducive to a kind of 'DLL hell' if used in a DLL's exported interface).

    Conversely, is it necessary to export the class declaration if one wishes to call statically defined member functions?

    If not the whole class, you need to export at least those individual static methods.

    0 讨论(0)
  • 2021-01-19 02:35

    Am I correct in believing that it is not necessary to mark that class as exported using __declspec if one is only going to call virtual or inline functions on its instances?

    Yes,this is correct, and that's what COM do, the DLL only expotys 4 methods, one of them returns to the class factory, which all its members are pure virtual functions.

    Conversely, is it necessary to export the class declaration if one wishes to call statically defined member functions?

    No, just export the static member functions.

    0 讨论(0)
  • 2021-01-19 02:36

    C++ name mangling serves as a burden to the success of writting cross-compiler modulars, simply declare the class you want to expose as an interface containing nothing but virtual functions. The layout of the class with virtual functions might be 'standardized', think of COM.

    0 讨论(0)
  • 2021-01-19 02:41

    It's not necessary only if the function/class have all it's definition with in a header file. It's not dependant on virtuality.

    So, if you don't export all your class, it's usable by client code as far as it don't have any public or protected function definition in a cpp file.

    You can also declare only specific member functions to be exported instead of the whole class, by using __declspec in the function declaration and not in the class name declaration.

    0 讨论(0)
提交回复
热议问题