如何将对象传递给C ++中的函数? [英] How to pass objects to functions in C++?

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

问题描述

我是C ++编程的新手,但我有Java的经验。我需要指导如何将对象传递给C ++中的函数。



我需要传递指针,引用或非指针和非引用值吗?我记得在Java中没有这样的问题,因为我们只传递保存对象引用的变量。



如果你也可以解释在哪里使用每个

解决方案

C ++ 11:



以值 传递 除非


  1. 你不需要对象的所有权,并且一个简单的别名将会这样做,在这种情况下你 传递 const 引用 strong>,

  2. 除非您必须变更物件,在这种情况下,使用 传递非 - const lvalue reference

  3. ,除非您将派生类的对象作为基类传递,在这种情况下,您需要 / em> 。 (使用先前的规则确定是否通过 const 引用。)

实际上从不建议传递指针。可选参数最好表示为 boost :: optional ,并且通过引用完成别名。



C + + 11的移动语义使得传递和返回值对于复杂的对象更有吸引力。






> C ++ 03:



通过传递参数 / em> ,除非


  1. 它们在函数中被更改,这些更改应反映在其中<

  2. 的函数应该是 callable没有任何参数,在这种情况下你传递指针,以便用户可以传递 NULL / 0 / nullptr 应用先前的规则以确定是否应通过 const 参数
  3. $ b的指针传递 $ b
  4. 它们是内置类型,可以通过复制

  5. 传递该功能和此类更改应反映在外面,在这种情况下您可以 通过复制


  6. (这里,pass by value是pass by value)称为通过复制,因为通过值总是在C ++中创建一个副本03)






    这,但这些几个初学者的规则会让你相当远。


    I am new to C++ programming, but I have experience in Java. I need guidance on how to pass objects to functions in C++.

    Do I need to pass pointers, references, or non-pointer and non-reference values? I remember in Java there are no such issues since we pass just the variable that holds reference to the objects.

    It would be great if you could also explain where to use each of those options.

    解决方案

    Rules of thumb for C++11:

    Pass by value, except when

    1. you do not need ownership of the object and a simple alias will do, in which case you pass by const reference,
    2. unless you must mutate the object, in which case, use pass by a non-const lvalue reference,
    3. unless you pass objects of derived classes as base classes, in which case you need to pass by reference. (Use the previous rules to determine whether to pass by const reference or not.)

    Passing by pointer is virtually never advised. Optional parameters are best expressed as a boost::optional, and aliasing is done fine by reference.

    C++11's move semantics make passing and returning by value much more attractive even for complex objects.


    Rules of thumb for C++03:

    Pass arguments by const reference, except when

    1. they are to be changed inside the function and such changes should be reflected outside, in which case you pass by non-const reference
    2. the function should be callable without any argument, in which case you pass by pointer, so that users can pass NULL/0/nullptr instead; apply the previous rule to determine whether you should pass by a pointer to a const argument
    3. they are of built-in types, which can be passed by copy
    4. they are to be changed inside the function and such changes should not be reflected outside, in which case you can pass by copy (an alternative would be to pass according to the previous rules and make a copy inside of the function)

    (here, "pass by value" is called "pass by copy", because passing by value always creates a copy in C++03)


    There's more to this, but these few beginner's rules will get you quite far.

    这篇关于如何将对象传递给C ++中的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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