c++classprivatesizeof

Visual Studio 2010 C++ compiler allocating wrong size for class


Currently I'm exporting a few classes from a DLL through dllexport and I'm making my private members private so the headers I provide for the DLL does not include anything private.

The problem is: now the compiler has no idea of the size of the class, it thinks I'm allocating an empty class (1 byte size), isn't that information supposed to be available in the .lib generated with the DLL? I can't hide private data?

To be clear, the DLL headers export the entire class using __declspec(dllexport) and the deployment headers __declspec(dllimport)


Solution

  • You can't do that for the reason you just found out. Your class without its private members is not the same class as your class with its private members. The compiler isn't allocating the wrong size, you're playing with two different classes that have the same name.

    Use something like the pimpl idiom to hide your class's privates. (See Opaque pointers, and The fast pimpl idiom).