I was curious about doing this in C++:
Lets say I have a small library that I distribute to my users. I give my clients both the binary and the associated header files that they need. For example, lets assume the following header is used in development:
#include <string>
ClassA
{
public:
bool setString(const std::string & str);
private:
std::string str;
};
Now for my question. For deployment, is there anything fundamentally wrong with me giving a ‘reduced’ header to my clients? For example, could I strip off the private section and simply give them this:
#include <string>
ClassA
{
public:
bool setString(const std::string & str);
};
My gut instinct says “yes, this is possible, but there are gotchas”, so that is why I am asking this question here. If this is possible and also safe, it looks like a great way to hide private variables, and thus even avoid forward declaration in some cases.
I am aware that the symbols will still be there in the binary itself, and that this is just a visibility thing at the source code level.
Thanks!
Modifying a class declaration this way will cause incompatible code.
The compiler needs the class declaration to figure out how much space an instance of that class takes and where in that space which member variables are located.
If you leave out members (even private ones) in one header, code compiled with this header will assume a different layout of the class. It will allocate less space for instances of the class and will assume the member variables are in incorrect places.