Reference initialization in C++

前端 未结 6 1575
挽巷
挽巷 2021-01-25 01:06

Greetings, everyone!

Examining my own code, I came up to this interesting line:

const CString &refStr = ( CheckCondition() ) ? _T(\"foo\") : _T(\"bar         


        
6条回答
  •  说谎
    说谎 (楼主)
    2021-01-25 01:47

    I can see now, that while CheckCondition() is executed, refStr exists, but still not initialized.

    From a language lawyer point of view, this is wrong. During initialization, refStr doesn't exist yet. I'd guess that your visual debugger is giving you misleading hints.

    If the code inside the initialization leads to an error condition, refStr will not exist, and will not ever have existed.

提交回复
热议问题