C++ problematic code piece

后端 未结 3 1542
孤城傲影
孤城傲影 2021-01-23 15:03

We got practice sheets for a test next week, for studying a little bit of C++ (still a beginner here). I still can\'t figure out a simple question.

That is, why are thes

3条回答
  •  余生分开走
    2021-01-23 15:40

    Both snippets pass a reference (or pointer in the second case) to temporary. Consider for example p(18). Where would 18 actually be stored? So where should p(18) point to?

    Side note: If everything would be const, the code would be ok, ie.

    int const & p(int const & input) { return input; }
    

    The standard would guarantee it.

提交回复
热议问题