Argument passing by reference to pointer problem

后端 未结 5 1239
你的背包
你的背包 2021-02-06 10:20

Every time I try to compile my code I get error:

cannot convert parameter 1 from \'int *\' to \'int *&\'

The test code looks like this:

5条回答
  •  长发绾君心
    2021-02-06 11:01

    &myval is an rvalue (of type int*), because it's a temporary. It's a pointer, but you cannot modify it, because it's just created on the fly. Your function set however requires a non-const reference, so you cannot pass it a temporary.

    By contrast, pMyVal is a named variable, thus an lvalue, so it can be passed as a non-constant reference.

提交回复
热议问题