temporary object, function parameters and implicit cast

前端 未结 3 1765
遇见更好的自我
遇见更好的自我 2021-02-11 05:20

In the following scenario:

struct Foo
{
   // ...
   operator Bar() {... }  // implicit cast to Bar
}

Foo GetFoo() { ... }
void CallMeBar(Bar x) { ... }

// ...         


        
3条回答
  •  萌比男神i
    2021-02-11 05:44

    As long as you return them by value, they survive until CallMeBar is finished.

    Your cast operator is a little off, though. Should be:

    struct Foo
    {
       // ...
      operator Bar() {... }  // implicit cast to Bar
    }
    

    See e.g. http://msdn.microsoft.com/en-us/library/ts48df3y(VS.80).aspx

提交回复
热议问题