How to implement a monkey patch in C ++?

Is it possible to implement a monkey patch in C ++? Or any other similar approach to this?

Thanks.

+3
source share
5 answers

To add to the other answers, think that any function that is opened through a shared object or DLL (depending on the platform) can be overridden at run time. Linux provides an environment variable LD_PRELOADthat can specify a shared object to load after all others, which can be used to override arbitrary function definitions. This is actually the best way to provide an “object layout” for unit testing purposes, as it is not invasive. However, unlike other forms of correction of monkeys, keep in mind that such a change is global. You cannot indicate that one particular call is different from another without affecting other calls.

+5
source

, - .

, , - . , , #include .

, " " - A B OS S, -, S , . ( , ).

, , . , VC, #import , _bstr_t . _bstr_t , , #define ' _bstr_t #import.

- VMT - -, . , VMT. , , , . , C++. / .

, / COM . , " ", .

+5

, , . , - ( , ). , , . , (, LD_LIBRARY_PATH) - , .

valgrind, , ( , ) .

+3

, " " , ++ :

  • const_cast const.
  • #define private public .
  • use Parent::protected_field .
  • .

, , , , , , .

+3

, , ++...

+2
source

Source: https://habr.com/ru/post/1720392/


All Articles