Is there a way to make passing by reference, and passing by value explicit in the function call?

前端 未结 7 1514
北荒
北荒 2021-02-18 13:43

If you were to look at this code,

int x = 0;
function(x);
std::cout << x << \'\\n\';

you would not be able to verify through any me

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-18 14:11

    I think it's something useless to notify (by language nonetheless [1]). The only needed question is : "Is my object is semantically modified ?", and so :

    • When you read a prototype you know if a function could modify an object (non-const ref) or not (copy or const ref).
    • When you use a function (even without reading [2] the prototype) if you have to be sure to not modify an object, use a const_cast.

    [1] A static analyzer could do it for its purposes.
    [2] If you miss, the compiler would warn you anyway.

提交回复
热议问题