Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

aop

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 this without actually editing every function, Is such a thing even possible?

I would settle for having a function called as the first instruction of every function call instead of it being called right before.

like image 823
Greg Avatar asked Jul 23 '09 19:07

Greg


People also ask

How will you call a function before main in C?

_start is always called before main , and in Windows, WinMain is called before main also.

Does order of code matter in C?

The order of functions inside a file is arbitrary. It does not matter if you put function one at the top of the file and function two at the bottom, or vice versa. Caveat: In order for one function to "see" (use) another function, the "prototype" of the function must be seen in the file before the usage.

Which function is executed first when we start the program and Cannot be called again?

The main function serves as the starting point for program execution. It usually controls program execution by directing the calls to other functions in the program.

How do you call a class function without object?

Only static class functions can be called without an object using the Class::function() syntax. So, you should add static as a keyword to the definition of your functions inside the Cat class.


1 Answers

AspectC++ is what you want. I haven't used it myself, but Aspect-Oriented Programming paradigm tries to solve this exact problem.

like image 155
notnoop Avatar answered Oct 14 '22 19:10

notnoop