calling "after return"...?

  • Thread starter .rhavin grobert
  • Start date
R

.rhavin grobert

[insert ya favorite greetin' here;-)]

guess you have the following classes...
___________________________________
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()?

TIA and AnyHelpGREATLYapreciated and ThanxForTheFish...

lg, -.rhavin;)
 
B

Branimir Maksimovic

[insert ya favorite greetin' here;-)]
Hi

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()?

It is not called from GetB, therefore does not
returns there.
Instead of returning B* directly, return
object by value that returns B* from overrided operator -> ,
and place your entry code in constructor, exit code
in destructor of that object.

Hope this helps.
Greetings, Branimir.
 
G

gw7rib

guess you have the following classes...
___________________________________
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()?

Possibly your best bet would be to replace GetB with a function to
call a method of B. Eg something like:

void A::callB(int fun) {
switch (fun) {
case METHODA: m_pB -> firstmethod(); break;
case METHODB: m_pB -> secondmethod(); break; }
doafterstuff();
}

For more advanced stuff, function pointers might help.

Hope this helps.
Paul.
 
B

Ben Bacarisse

.rhavin grobert said:
[insert ya favorite greetin' here;-)]

guess you have the following classes...
___________________________________
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,

Obviously you put it after the call you've just written above. Now,
since this is obvious there must be some confusion...
e.g. when the callstack - after completing the
call into B - returns back into A::GetB()?

And here it is. That does not happen. The A.GetB() call is entirely
over by the time AMethodOfB is called.
 
P

Pascal J. Bourguignon

.rhavin grobert said:
[insert ya favorite greetin' here;-)]

guess you have the following classes...
___________________________________
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.
 
E

Eric.Malenfant

[insert ya favorite greetin' here;-)]
Hi

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()?

It is not called from GetB, therefore does not
returns there.
Instead of returning B* directly, return
object by value that returns B* from overrided operator -> ,
and place your entry code in constructor, exit code
in destructor of that object.

Stroustrup wrote a paper about this technique a while ago: "Wrapping C+
+ Member Function Calls": http://www.research.att.com/~bs/wrapper.pdf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top