Is it legal to pass a newly constructed object by reference to a function?

后端 未结 9 2084
刺人心
刺人心 2020-12-31 04:44

Specifically, is the following legal C++?

class A{};

void foo(A*);
void bar(const A&);

int main(void)
{
    foo(&A());  // 1
    bar(A());  // 2
}

It a

9条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-31 05:37

    Perfectly legal.

    The object will exist on the stack during the function call, just like any other local variable as well.

提交回复
热议问题