function question
Lets say I have a function which is called by an object. Is there any way to know what object called the function? Apart from simply passing a memory address when you call the function?
Most languages have some kind of "self" or "this" reference. For example, in C++ "this" is a pointer to the current object. In Java, "this" is an object reference. In Lua "self" is an object/table reference.
I'm using C / C++.
I don't have any pseudo code atm, but I have a feeling there isn't a way other than passing some information about the caller as a parameter.
I don't have any pseudo code atm, but I have a feeling there isn't a way other than passing some information about the caller as a parameter.
Quote:Original post by scottrick49
I'm using C / C++.
I don't have any pseudo code atm, but I have a feeling there isn't a way other than passing some information about the caller as a parameter.
Hold on, so you want something like this?
class Receiver { void receive() const;};class Sender { void sendTo(const Receiver&);};void Sender::sendTo(const Receiver& r) { r.receive();}void Receiver::receive() const { // which Sender instance called me?}
Yes, you would have to pass the Sender explicitly (accept a const Sender& in receive(), and have sendTo pass '*this' as the parameter when making the call). Keep in mind that as it stands, the "caller" doesn't have to be a Sender object - or for that matter, *any object at all*; you could do "Receiver().receive()" directly in main(), for example.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement