I noticed some weird behavior that I can't figure out. I have function that takes ?&in argument, and as soon as I pass "this" pointer there, it calls the destructor even if I don't do anything in a called function. What it seems to do is to actually create another instance of an object and that's the one that goes into the function, not the original handle. Is there something I don't get about using ?&in ?
class Foo
{
Foo() { print "Foo();\n"; }
~Foo() { print "~Foo();\n"; }
void bar() { print("start\n"); call(this); print("stop\n"); }
}
Foo f;
f.bar();
and the C++ function "call" is:
void Call(void* obj, int type_id)
{
std::cout << "call()" << std::endl;
}
engine->RegisterGlobalFunction("void call(?&in)", asFUNCTION(Call), asCALL_CDECL); assert( r >= 0 );
the result? after calling "call(this)" a new object is created, and destroyed as soon as the Call() function exits. So it actually prints this:
Foo()
start
Foo()
call()
~Foo()
stop
~Foo()
where that first ~Foo() is where new object is created. I guess that if I tried to modify object on C++ side, using that *obj pointer, it would be the object that is gone as soon as the function exits, not the one I think I'm modifying.
So the question is - should it make a "copy"? (I suppose that it actually does a copy of the object, because when I set some foo._value to "X" and default one was empty, the destructed object had it set to "X", indicating a copy rather than new object.
And how to prevent this copy? Actually it would be nice if I could decide if I want to pass a copy of an object vs. reference to an object in such case - is it possible with "?" notation, so it would work for any object?