Declare a reference and initialize later?

后端 未结 10 2146
遥遥无期
遥遥无期 2020-12-01 05:58

I have a reference to MyOjbect, but the the exact object depends on a condition. So I want to do something like this:

MyObject& ref; 
if([co         


        
10条回答
  •  一个人的身影
    2020-12-01 06:46

    You need to initliaze it. But if you would like to conditionally initialize it, you can do something like this:

    MyObject& ref = (condition) ? MyObject([something]) : MyObject([something else]);
    

提交回复
热议问题