Thread View: de.comp.lang.c++
4 messages
4 total messages
Started by Armin M. Warda
Thu, 10 Aug 1995 10:02
Aufrufer einer Methode feststellen?
Author: Armin M. Warda
Date: Thu, 10 Aug 1995 10:02
Date: Thu, 10 Aug 1995 10:02
53 lines
1935 bytes
1935 bytes
Hallo,
gibt es eine Moeglichkeit, den Aufrufer einer Methode
in der Methode festzustellen?
Ich koennte natuerlich beim Aufruf einer Methode immer
den 'this'-Zeiger explizit uebergeben:
class X
{
public:
void f(int x, Y* caller=NULL)
{
if (caller)
if (caller == ...)
...
caller->...;
...
else
...
else
...
};
};
Aufruf aus einer Methode eines anderen Objekts:
f(42, this);
Aufruf aus main() oder einer anderen Funktion, die
keine Methode eines Objekts ist:
f(42);
Muss ich tatsaechlich den 'this'-Zeiger des Aufrufers
explizit uebergeben? Gibt es sowas wie einen impliziten
'caller'-Zeiger (analog zum 'this'-Zeiger) bzw. eine
einfache Moeglichkeit, sowas zu implementieren?
Die benoetigte Information muesste ja eigentlich auf dem
Stack vorhanden sein, ein Debugger (beispielsweise GDB)
hat ja auch keine Probleme, die Stack-Frames hoch- ('UP')
und runterzulaufen ('DOWN'), um dann auf die jeweiligen
'this'-Zeiger zuzugreifen.
Ich verwende GCC 2.5.8 - 2.6.3 unter SunOS 4.x - 5.x
und Linux 1.2.x - 1.3.x.
A.
--
-------------------------------------------------------------------------------
Armin M.Warda <http://ls4-www.informatik.uni-dortmund.de/QM/MA/warda/pi.html>
Universitaet Dortmund, Fachbereich Informatik, Lehrstuhl IV, D-44221 Dortmund
Re: Aufrufer einer Methode feststellen?
Author: Uwe Behrens
Date: Thu, 10 Aug 1995 12:11
Date: Thu, 10 Aug 1995 12:11
18 lines
1243 bytes
1243 bytes
In article <40clg8$oej@fbi-news.informatik.uni-dortmund.de>, warda@veronica.informatik.uni-dortmund.de (Armin M. Warda) writes:
|> Hallo,
|>
|> gibt es eine Moeglichkeit, den Aufrufer einer Methode
|> in der Methode festzustellen?
|>
|> Ich koennte natuerlich beim Aufruf einer Methode immer
|> den 'this'-Zeiger explizit uebergeben:
[ Rest geloescht ]
Reine Neugierde: Wozu brauchst Du sowas?
________________________________________________________________________________
Uwe Behrens <behrens@acm.org>
``I'm not convinced Motif is compliant with itself.''
________________________________________________________________________________
Re: Aufrufer einer Methode feststellen?
Author: Martin Cracauer
Date: Thu, 10 Aug 1995 13:53
Date: Thu, 10 Aug 1995 13:53
32 lines
1885 bytes
1885 bytes
warda@veronica.informatik.uni-dortmund.de (Armin M. Warda) writes:
>Muss ich tatsaechlich den 'this'-Zeiger des Aufrufers
>explizit uebergeben? Gibt es sowas wie einen impliziten
>'caller'-Zeiger (analog zum 'this'-Zeiger) bzw. eine
>einfache Moeglichkeit, sowas zu implementieren?
Du musst. So was kann nicht mal CLOS :-)
>Die benoetigte Information muesste ja eigentlich auf dem
>Stack vorhanden sein, ein Debugger (beispielsweise GDB)
>hat ja auch keine Probleme, die Stack-Frames hoch- ('UP')
>und runterzulaufen ('DOWN'), um dann auf die jeweiligen
>'this'-Zeiger zuzugreifen.
Schon, aber das ist portabel nicht zu machen. Der gcc hat einige
"Meta"-Erweiterungen, mit denen Informationen dieser Art
bereitgestellt werden (z.B. den Namen der aktuellen Funktion), aber
nicht diese.
BTW, ich verstehe nicht ganz, wie Du festlegen willst, fuer welches
Object Du einen Zeiger uebergebn willst. Vermutlich das, in deren
Klasse die Memberfunction definiert ist. C++ ist aber eine statisch
getypte Programmiersprache. Wenn Du einen solchen Zeiger uebergeben
willst, wuerde das bedeuten, dass Du die aufzurufende Funktion nur
noch von Objecten eines Typs callen kannst, was sicherlich eine
heftige Einschraenkung ist.
--
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Martin Cracauer <cracauer@wavehh.hanse.de>. No NeXTMail, please.
Norderstedt/Hamburg, Germany. Fax +49 40 522 85 36. This is a
private address. At (netless) work programming in data analysis.
Re: Aufrufer einer Methode feststellen?
Author: Armin M. Warda
Date: Fri, 11 Aug 1995 12:19
Date: Fri, 11 Aug 1995 12:19
81 lines
3086 bytes
3086 bytes
Stuart Maclean <S.D.Maclean@ecs.soton.ac.uk> schrieb mir
zur gleichen Anfrage in comp.lang.c++:
: In article <40cm1r$oej@fbi-news.informatik.uni-dortmund.de>,
: warda@veronica.informatik.uni-dortmund.de (Armin M. Warda) wrote:
: > Hello,
: >
: >is there a possibility to find out within a method who
: >called the method?
: >
: >I could explicitly give the 'this'-pointer as parameter
: >when calling a method:
: Isn't it neater when you dont have to explicitly pass this around 8-)
: Unfortunately, if YOU don't supply the info, the runtime system will have
: to do more work to get the caller object. What with looking at the stack
: and stuff, you're going to need to know your architecture/compiler quite
: well.
: Here's what I do to identify the calling object on the SPARC, compiler: GCC
: .text
: .align 4
: .global _caller
: _caller:
: mov %g1, %o1 ! %o1 is scratch, so OK to save %g1 there
:
: restore ! back to caller's register window (cached stack frame)
:
: mov %i0, %g1 ! GCC passes the 'this' pointer in %o0, which becomes
: ! %i0 in the new window. Grab it into %g1
:
: save ! back to the original window
:
: mov %g1, %o0 ! return value (ie the calling object) returned in %o0
:
: retl ! return to caller. Note this function is a LEAF !!!
:
: mov %o1, %g1 ! restore clobbered %g1.
: ! This is the delay slot instruction.
:
: ! end of _caller
: Note that this ASSUMES that there was indeed an object address in %i0 of
: the previous stack frame. In other words, we assume that the calling
: function was indeed a non-static member function, not an ordinary function
: or static member function.
: Then use as such
: class Foo {
: public:
: void Bar();
: };
:
: void Foo::Bar()
: {
: if ( caller() == this ) {
: this is an intra-object call....
: }
: NOTE : As per usual when comparing object equality by way of address
: equality, beware of multiple inheritance. For a discussion, see Meyers,
: item 17, page 57.
: But who ever uses MI anyway ;-)
: Hope this helps. Any comments welcomed.
: Later,
: Stuart
A.
--
-------------------------------------------------------------------------------
Armin M.Warda <http://ls4-www.informatik.uni-dortmund.de/QM/MA/warda/pi.html>
Universitaet Dortmund, Fachbereich Informatik, Lehrstuhl IV, D-44221 Dortmund
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads