is there a way to get the pointer to the object which called your function?

ideally something like:

1
2
3
void testfunc(int x = 0, void called = this){
    called->stuff(x);
} 


which obviously doesn't compile but is there a way to get the pointer of the object (class) that called the function and then use that in the code of the custom function?
yeah just pass it as a pointer of reference.

1
2
3
4
5
6
class MyClass{
    public:
        void testFunc(MyClass* caller);
        // or rather than a pointer, I suggest you pass by const reference
        void testFunc(const MyClass& caller);
};


if the calling object needs to be modified then you can't pass a const reference of course, but if possible make it const
Last edited on
well that would work if I knew what the class was going to be for sure. but this function can run on a variety of classes. It really needs to be treated almost like a void type...

I'm not even sure this is possible...

thanks for the reply though
Last edited on
what exactly do you need to do with the calling object?
1
2
3
4
5
template<typename T>
int SomeFunc(const int &Param, /*const*/ T &caller)
{
  //code here
}
Last edited on
LB:

that code says it's unable to resolve the address of caller whenever I try to use it. Plus you have to specify the object I am calling the function from which I was trying to avoid doing.

But I'm basically calling a function from a window, that does some stuff to the window hence I need the pointer too it.

The catch is there are different types of these windows I'm calling (some not even "windows" at all), so just declaring the function as a member of the window won't work. I need a way to get the window pointer on the fly. I was hoping there was a way to do this without having to specify it each time. And since it will almost always be done on the window that calls it I was hoping for a way to set the window that called it as the default pointer, instead of NULL or something just as useless.
that code says it's unable to resolve the address of caller whenever I try to use it.
What?

Plus you have to specify the object I am calling the function from which I was trying to avoid doing.
No, you don't - the compiler can figure that out for you.


Sorry, but what you want will have to be done manually.
Last edited on
I was afraid of that... well thanks for trying folks.
closed account (1yR4jE8b)
This is a design issue,

If you need to know the caller, the function should be a member function of that class so you can use the this pointer, instead of a free function.
yes, except the classes I'm using are coming from a library I'm using. So I could recreate them in a custom class but that seems a bit hardcore...

I'm currently trying to wrap them in a custom class, but I'm not sure how well that is going to work, we'll see, might even make things more flexible for all I know. Never came across this situation before.
Topic archived. No new replies allowed.