Since this is misunderstood, I must clarify. All of the following solutions do not require recompiling the object . To use a class in code if it is compiled into an object file, you must include a header file with the declaration of that class.
#include <class.h> ObjectFoo instance;
It is possible (but dangerous if you are not careful) to change the title (a) or copy the title to another location and include this title (b), without recompiling the class .
#include <class_fixed.h> ObjectFoo instance;
Your code, in which you include the new header, will just think that in the object file (which you did not recompile!) It will find an implementation of the class declared as in class_fixed.h . While the class declared as class.h . If you change member offsets (for example, add new members) to your new heading, you are dead and the code will not work properly. But just changing access works fine. Compiled code does not know about access, it only matters when compiling.
This is not always harmful. In everyday life, you encounter such a change when installing a new version of the library in your system and do not recompile all programs that depend on it. But he should be handled with care.
There are several solutions.
memcpy()
Do not! Not memcpy, since copying objects is sometimes subject to certain policies imposed by the class designer. For example, auto_ptr s cannot just be memcopied: if you memcopy auto_ptr and then the destructor runs for both, you will try to free the same memory twice and the program will crash.
Change private: to public: in the header or using a macro. If your license resolves it, you can solve your problem by editing the header file that comes with the class implementation . Whether the source code of the implementation (i.e., the Cpp class file) is under your control does not matter: changing private to public for data members (in the header) is enough and works fine even if you are provided with binary, only a library containing the class definition . (For member functions that change access, its internal name sometimes changes, but for MSVS and GCC this is normal.)
Adding a new getter function. Changing private to public almost always normal (unless you rely on specific compile-time checks that should break the compilation if the class has a specific member available) adding a new getter function should be done carefully. The getter function must be built-in (and therefore defined in the class header file).
reinterpret_cast
The effect differs only if you DO NOT throw a pointer to a dynamic base class (dynamic tools) with virtual functions or bases "), the actual instance of which at the time of casting can be obtained from the class in a specific code fragment.
protected:
And just in case you forgot. C ++ can declare protected: members, i.e. Available only for classes derived from this. It can satisfy your needs.
Pavel shved
source share