finding the caller of a function

hey all you people out there!

how can i refer to the caller of a function?
i used as3.0 before i decided to start with c++ and i was used to make a function which refers to the caller with the this keyword.

i should look something like this:

void myFunction () {
caller.myVariable = x+y;
}
myObject.myFunction();
You can't. The way to do this in C++ is to make a class/member functions:

http://www.cplusplus.com/doc/tutorial/classes/
Ok, thanks anyway then.
The whole point of a function is to separate the caller and the function. That is, functions shouldn't know who/what/where is calling them. All a function should do is its own thing. The way that function is used is up to the caller.

That's why functions have return values and reference arguments, to return values back to the caller. And all other arguments are to provide the function with values from the caller.

Hope this helps.
Yes i know that,
but i find it easier to use something like a reference to the caller instead of supplying the caller as an argument.
Probably just because i'm used to it, but i can live without.
If you know that, then why are you trying to defeat it?
Because i was used to such a luxury (i find) in as3.0 and i was curious wether i could use it in c++ as well.
Yeah, it is better just to learn to do things the Right Way -- you'll improve your coding skills and stuff will just work better. :-)
Tilpo:

The reason this isn't provided in, well, really any modern programming language is that it breaks encapsulation. A function should be callable from anywhere. Requiring the caller to have specifically named variables just for one function breaks that encapsulation. Now, every function ever written has to know the variables that every other function may want to modify in the caller of a function. Otherwise functions can step on one another's data. A better way has been found and many (most?) modern programming languages use it. Objects encapsulate data and the functions that operate on that data. Class methods (functions) can operate on the data that belong to the class to which the function belongs.
I understand.
Another thing then, if possible, how can i add a function to a class in the standard libary. For example the string object?
Generally, no. Few of the types and containers defined in the the standard library are designed to be extended in this manner. The extension mechanism provided is through algorithms. See the Boost String Algorithms Library for an example. Many would say that C++'s string type has too many member functions already, and that the library should have provided fewer member functions and a more robust set of string algorithms at the outset.

Topic archived. No new replies allowed.