Is it possible to overload object so that it does something if passed as an argument?

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;
}
I'm not entirely sure what you want to do.
Why exactly do you want to do this?
What exactly do you wish to accomplish?
are you asking if an object can detect that it was passed?
Yes, @jonnin.

This is what I would want to be able to do:

1
2
3
4
5
6
7
8
9
10
11
12
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.
No, the object is not aware of being passed by reference.

do something because of it

And since what you want to do is apparently a secret, there's nothing we can do to help you with a more feasible approach.
Last edited on
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*
*gives finger*
> When the foo object is passed as an argument, I want it to be able to detect that it was passed

We can have the function accept a wrapped reference;
a wrapper that does something with the object when it is constructed.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <utility>

template < typename T > struct reference
{
    constexpr reference( T& t ) noexcept( noexcept( std::declval<T>().do_something() ) )
        : object(t) { object.do_something() ; }

    template < typename THAT >
    constexpr reference( reference<THAT>& that ) noexcept( noexcept( std::declval<T>().do_something() ) )
        : object(that.object) { object.do_something() ; }

    constexpr operator T& () const noexcept { return object ; }

    T& object ;
};

struct foo
{
    void do_something() { std::cout << "foo::do_something (non-const)\n" ; i = 999 ; }
    void do_something() const { std::cout << "foo::do_something (const)\n" ; }

    int i = 23 ;
};

void some_function( reference<const foo> fooey )
{
    const foo& arg = fooey ;
    std::cout << "some_function - arg.i == " << arg.i << '\n' ;
}

void another_function( reference<foo> fooey )
{
    foo& arg = fooey ;
    // ...
    some_function(fooey) ;
}

int main()
{
    const foo a_foo{1} ;
    some_function(a_foo) ;

    foo another_foo{2} ;
    another_function(another_foo) ;
}

http://coliru.stacked-crooked.com/a/c619c69d2f273d96
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.
JLBorges wrote:
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.
Last edited on
Topic archived. No new replies allowed.