Run Code Before Every Function Call for a Class in C++

后端 未结 9 1235
被撕碎了的回忆
被撕碎了的回忆 2021-02-15 13:08

I would like to run some code (perhaps a function) right before every function call for a class and all functions of the classes that inherit from that class. I\'d like to do t

9条回答
  •  失恋的感觉
    2021-02-15 13:23

    I would suggest using the Non Virtual Interface idiom. All public functions are non-virtual. All virtual functions are protected or private. Public members delegate the calls to virtual members and are usually implemented as inline functions.

    This is the way IOStreams are implemented in STL. You can read more about it at C++ Wikibooks.

    Intent: To modularize/refactor common before and after code fragments (e.g., invariant checking, acquiring/releasing locks) for an entire class hierarchy at one location.

    Regards,
    Ovanes

提交回复
热议问题