473,379 Members | 1,235 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

calling "after return"...?

[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;)
Jun 27 '08 #1
5 1542
On May 5, 3:03*pm, ".rhavin grobert" <cl...@yahoo.dewrote:
[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.

Jun 27 '08 #2
On 5 May, 14:03, ".rhavin grobert" <cl...@yahoo.dewrote:
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.
Jun 27 '08 #3
".rhavin grobert" <cl***@yahoo.dewrites:
[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.

--
Ben.
Jun 27 '08 #4
".rhavin grobert" <cl***@yahoo.dewrites:
[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.
--
__Pascal Bourguignon__
Jun 27 '08 #5
On May 5, 10:28 am, Branimir Maksimovic <bm...@hotmail.comwrote:
On May 5, 3:03 pm, ".rhavin grobert" <cl...@yahoo.dewrote:
[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
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Ximo | last post by:
Hello, I want that the return sentence don't return anything, how can I do it?. If i do only return it returns None, and pass don't run too. Can anyone help me?, thanks. XIMO
6
by: JS | last post by:
In this sample code: if(n==0&&args>1){ for(i=num;args>i+1;i++){ s.length = 0; opt = document.createElement('OPTION'); s.appendChild(opt); opt.value = ""; opt.text = "\74-- Vælg --"; }
10
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has occurred during behind-the-scenes cleanup of some...
3
by: Trevor Andrew | last post by:
Hi There, I have a small ASP.NET application under development. I am using VS.NET 2002 (2003 upgrade is on the way) with .NET Framework 1.1. It is hosted on a web hosting service in the US. I am...
13
by: Bob Day | last post by:
Using vs2003, vb.net I start a thread, giving it a name before start. Code snippet: 'give each thread a unique name (for later identification) Trunk_Thread.Name = "Trunk_0_Thread" ' allow...
1
by: vl106 | last post by:
char* foo () { return "abc"; } I compiled the above code both with MSVC and GCC for PPC. The string "abc" is generated as a global entity. Thus (1) foo doesn't return a temporary and (2) no...
32
by: Axel Bock | last post by:
Hi all, I am trying to get my head around what happens if I return a class object from a function. It seems C++ (MinGW) does not invoke the copy constructor if I do something like this: ...
13
by: Steve | last post by:
On page 392 of "Javascript the definitive guide" a function is called like this:- <form action="processform.cgi" onsubmit="return validateForm();"> Why, in this instance, is the return...
15
by: Steve | last post by:
I am having problems getting values out of an array. The array is set as a global array and values are pushed into it as they are read from a JSON file using a "for loop". When the "for loop" is...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.