Jump to content
  • Advertisement
Sign in to follow this  
scottrick49

function question

This topic is 4143 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!