tags:

views:

335

answers:

8

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.

+6  A: 

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

notnoop
I am strictly against Aspects, because you never know how much cost they bring to your code and to which code these will expand. It is also not clear how to debug these in C++, because they effectively produce code and might entirely change entry points of your functions.
ovanes
+1  A: 

The somewhat inconvenient way where to build a wrapper class that takes an object of your base type and calls the surrounding function and then the function that you wanted to call. This would be something like a decorator.

Janusz
A: 

This sounds like what a profiler does. Have you looked at the source for any profiling tools?

Jay
Sounds more like contractual programming. Checking pre and post conditions to validate that the contract has not be broken.
Martin York
+3  A: 

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

ovanes
A: 

The best you can do is to declare a set of virtual functions as protected and have the developers inheriting from the class override the virtual functions. The interface used by the base class can be public, which executes the desired code before passing information to the protected virtual method.

For example:

class Base {
  public:
    void MyMethod(void) { /* Insert code here */ YourMethod(); }
  protected:
    virtual void YourMethod(void) {}
};

If the developer knows that he has a specific subclass, he can still bypass your code simply by using a dynamic_cast, and using his own method set. As such, you may want to follow the other suggestions already posted that do not involve the base C++ language.

Raymond Martineau
I can think of other ways that don't involve invading the class being tested.
Martin York
+1  A: 

Another thing you could consider is using something like the [boost/C++0X] shared_ptr wrapper, where you call your custom function on the '->' overload before returning the class instance pointer. It involves modifying usage but not the underlying class, and I've used it a couple times to achieve the same effect. Just another thought.

Nick
This sounds like excellent alternative.
Greg
+2  A: 

The following might be a bit of an overkill - but how about?

http://msdn.microsoft.com/en-us/library/c63a9b7h.aspx

ossandcad
A: 

You could also do this with the Curiously recurring template pattern (CRTP).

ceretullis