Is it possible to overload some operator of a class such that it will do something if passed as an argument? Like if I instantiated Foo foo in main() and then passed it as an argument for someFunction() that takes objects of class Foo, would I be able to make it so that the object would do something? For example: setting one of its private variables to a certain integer value?
1 2 3 4 5 6 7 8 9 10 11
class Foo {
public:
Foo() {
num = 5;
}
~Foo(){}
private:
int num;
}
int main()
{
Foo foo;
//object passed as argument
someFunction(foo);
}
void someFunction( Foo &fooey )
{
//Does something
}
When the foo object is passed as an argument, I want it to be able to detect that it was passed and do something because of it. It would do something when passed and then someFunction() would execute its set of statements.
It isn't a secret. I just don't think the context matters. If it isn't possible then there are other things I am going to try. *shrugs*
You're objectively bad at asking questions. If you really want to know things, you need to get better at it. It's your choice, of course; if your self-image as a line of text on a screen read by strangers is more important to you than actually getting answers, that's a choice you can make.
We can have the function accept a wrapped reference;
a wrapper that does something with the object when it is constructed.
Semantically, the function still calls the shots there.
We decide between:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// A: call hidden in wrapper
void another_function( reference<foo> fooey )
{
foo& arg = fooey ;
// body
// B: explicit call
void another_function( foo& arg )
{
arg.do_something();
// body
// C: do nothing
void another_function( foo& arg )
{
// body
vaderboi wrote:
When the foo object is passed as an argument, I want it to be able to detect that it was passed
There are no objects in the binary. There are just instructions that are executed. If the "passer" does not shift the execution to your instructions, then there is nothing that you can do.
vaderboi wrote:
there are other things I am going to try
In other words you will continue creating XY problems.
there may be some way to keep an always on thread for each class instance (created in ctor and ended via dtor) that could do it, eg monitoring the call-stack at the asm level. It would be horrid, though.. the thread(s) would waste a lot of resources and the solution may not be portable. It sounds like you might like a callback type idea. Or some sort of event-driven idea.