views:

153

answers:

2

Hi,

On page 57 of The Design and Evolution of C++, Dr. Stroustrup talks about a feature that was initially part of C with Classes, but it isn't part of modern C++(standard C++). The feature is called call/return. This is an example:

class myclass
{
  call() { /* do something before each call to a function. */ }
  return() { /* do something else after each call to a function. */ }
  ...
};

I find this feature very interesting. Does any modern language have this particular feature?

+3  A: 

Aspect Oriented Programming has this. http://en.wikipedia.org/wiki/Aspect-oriented_programming

Aspect Oriented Programming (also known as AOP) has the ability to create interceptors before, after and around code.

Romain Hippeau
+8  A: 

The modern C++ equivalent would be a sentry object: construct it at the beginning of a function, with its constructor implementing call(), and upon return (or abnormal exit), its destructor implements return().

Potatoswatter
Are not sentry objects just for input and output streams ?
Romain Hippeau
+1 Thanks, I didn't even know about this class in C++. Hopefully I'll try to implement my own!
AraK
@Romain: you can make sentry objects for anything you want. I use them frequently while debugging, and to ensure invariants are maintained. They're also frequently employed to help ensure functions are properly exception safe.
Dennis Zickefoose
Sentry objects are very similar indeed. On the one hand they require explicit instantiation (and being passed `this`) but on the other hand you can add to them so that they check not only the invariants of the class but some pre/post conditions for the function at hand.
Matthieu M.
http://stackoverflow.com/questions/2690851/want-to-understand-c-sentry-object
Romain Hippeau
This is not really a language construct, but an idiom
Romain Hippeau
@Romain: correct. When you implement it, though, it works perfectly.
Potatoswatter