将可修改的参数传递给c ++函数 [英] Passing a modifiable parameter to c++ function

查看:194
本文介绍了将可修改的参数传递给c ++函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提供时,我想向函数传递一个可修改的参数,我应该选择什么:通过指针传递或通过引用传递?


  1. bool GetFoo(Foo& whereToPlaceResult);

  2. bool GetFoo(Foo * whereToPlaceResult);

我问这是因为我一直认为它是通过引用(1)传递参数的最佳实践,但是在检查了一些本地代码数据库后,我得出一个结论,最常见的方法是(2)。此外,男人自己(Bjarne Stroustrup)建议使用(2)。

解决方案

我更喜欢使用引用而不是指针:




  • 不能为空


  • 不能删除(由接收指针的人)



有些人说虽然引用和const引用之间的区别对于许多人来说太微妙,并且在调用该方法的代码中是不可见的(即,如果你读取调用代码它通过引用传递一个参数,你不能看到它是一个常量或非常量引用),因此你应该使它成为一个指针(使它在调用代码中显式,你给出的地址



我个人更喜欢一个引用,原因如下:


  1. 我认为一个例程应该知道它所调用的子例程

  2. 子例程不应该假设什么例程


  3. [1.]意味着让调用者可见的可变性无关紧要,因为调用者应该已经(通过其他方式)了解子程序的作用(包括它将修改参数的事实)。



    [2。]意味着如果它是一个指针子程序应该处理参数为空指针的可能性,这可能是额外的和IMO无用的代码。



    此外,每当我看到一个指针,要删除这个,当什么时候?,所以每当/所有权/生命周期/删除不是一个问题,我更喜欢使用引用。



    我习惯于编写const正确的代码:所以如果我声明一个方法有一个非const引用参数,它的非常量的事实是重要的。如果人们没有写出const正确的代码,那么或许很难判断一个参数是否会在子程序中被修改,而另一个机制(例如指针而不是引用)的参数会更强。 / p>

    Provided, I want to pass a modifiable parameter to a function, what should I choose: to pass it by pointer or to pass it by reference?

    1. bool GetFoo ( Foo& whereToPlaceResult );
    2. bool GetFoo ( Foo* whereToPlaceResult );

    I am asking this because I always considered it the best practice to pass parameter by reference (1), but after examining some local code database, I came to a conclusion, that the most common way is (2). Moreover, the man himself (Bjarne Stroustrup) recommends using (2). What are the [dis]advantages of (1) and (2), or is it just a matter of personal taste?

    解决方案

    I prefer a reference instead of a pointer when:

    • It can't be null
    • It can't be changed (to point to something else)
    • It mustn't be deleted (by whoever receives the pointer)

    Some people say though that the difference between a reference and a const reference is too subtle for many people, and is invisible in the code which calls the method (i.e., if you read the calling code which passes a parameter by reference, you can't see whether it's a const or a non-const reference), and that therefore you should make it a pointer (to make it explicit in the calling code that you're giving away the address of your variable, and that therefore the value of your variable may be altered by the callee).

    I personally prefer a reference, for the following reason:

    1. I think that a routine should know what subroutine it's calling
    2. A subroutine shouldn't assume anything about what routine it's being called from.

    [1.] implies that making the mutability visible to the caller doesn't matter much, because the caller should already (by other means) understand what the subroutine does (including the fact that it will modify the parameter).

    [2.] implies that if it's a pointer then the subroutine should handle the possibility of the parameter's being a null pointer, which may be extra and IMO useless code.

    Furthermore, whenever I see a pointer I think, "who's going to delete this, and when?", so whenever/wherever ownership/lifetime/deletion isn't an issue I prefer to use a reference.

    For what it's worth I'm in the habit of writing const-correct code: so if I declare that a method has a non-const reference parameter, the fact that it's non-const is significant. If people weren't writing const-correct code then maybe it would be harder to tell whether a parameter will be modified in a subroutine, and the argument for another mechanism (e.g. a pointer instead of a reference) would be a bit stronger.

    这篇关于将可修改的参数传递给c ++函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆