I'm puzzling over the following code, which (surprisingly, to me) compiles:
class A {
int a=0;
};
A returnsA(void)
{
static A myA;
return myA;
}
void works(void)
{
A anotherA;
returnsA() = anotherA;
}
I can't find anything in the standard or on the web which suggests that it shouldn't compile. It just seems very weird, to me.
I guess returnsA()
is returning an object (a copy of myA
), so we invoke the default copy assignment operator on it, anotherA
gets copy-assigned to the returned object, which then goes out of scope and is destroyed.
I was expecting behavior more like this, which doesn't compile:
int returnsint(void)
{
static int i=0;
return i;
}
void doesntwork(void)
{
int anotherint=0;
returnsint() = anotherint;
}
Can anybody enlighten me further about this behavior?