".rhavin grobert" <clqrq@[EMAIL PROTECTED]
> writes:
> [insert ya favorite greetin' here;-)]
>
> guess you have the following cl*****...
> ___________________________________
> class {
> //..a couple of methods ...//
> };
>
> class A {
> public:
> //...//
> B* GetB() // <- this we'll talk about
> private:
> B* m_pB;
> };
>
> A::GetB() {
> //..some code..//
> return m_pB;
> };
> ___________________________________
>
> when some does a...
>
> A.GetB()->AMethodOfB();
>
> ... and you want some code be executed before B::AMethodOfB() is
> called, you simply pack it into A::GetB()'s body before the return.
> But what do yuo do, if you want to execute some code _after_ B's
> method is executed, e.g. when the callstack - after completing the
> call into B - returns back into A::GetB()?
If you really want to do that kind of thing, what you are really
wanting is CLOS, the Common Lisp Object System. Yes, that means you
won't be programming in C++ anymore, but in Lisp. Here it is _*easy*_:
(defmethod a-method-of-b :before ((self b))
;; code to be executed before the main a-method-of-b is called
)
(defmethod a-method-of-b :around ((self b))
;; code to be executed around the main a-method-of-b is called
;; before
(+ (call-next-method) ;; calls the next a-method-of-b.
;; after
42))
(defmethod a-method-of-b :after ((self b))
;; code to be executed after the main a-method-of-b is called
)
Of course, you can always painfully try to replicate that in C++...
class A {
public:
typedef B::result_type (B::* method_type)(B::argument_type);
virtual void doSomethingBefore();
virtual result_type around(B* target,method_type
method,B::argument_type arg);
virtual void doSomethingAfter();
}
B::result_type A::around(B* target,method_type method,B::argument_type
arg){
this->doSomethingBefore();
B::result_type result=(target->*method)(arg);
this->doSomethingAfter();
return(result);
}
B* myB;
A* myA;
a->around(myB,&B::AMethodOfB,someArg);
a->around(myB,&B::anotherMethodOfB,someOtherArg);
And you can even have some fun with templates.
--
__Pascal Bourguignon__


|