Create objects in conditional c++ statements

前端 未结 7 1259
悲哀的现实
悲哀的现实 2020-12-06 01:33

I am learning c++, and I just got to the object oriented chapter. I have a question about creating objects inside if statements.

The problem I\'m working on says

相关标签:
7条回答
  • 2020-12-06 02:02

    You can use an rvalue reference to bind in place to either constructed object.

    struct Foo
    {
      Foo(int bar): bar(bar) {}
    
      int bar
    };
    
    Foo&& f = condition ? Foo(4) : Foo(5);
    
    f.bar = 1000; 
    
    0 讨论(0)
提交回复
热议问题