274

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.

Rakesh K
  • 8,237
  • 18
  • 51
  • 64
  • 7
    Which book are you learning C++ from? –  Jan 26 '10 at 12:09
  • 1
    I am using Complete Reference:C++ by Herbert Schildt. – Rakesh K Jan 26 '10 at 12:11
  • 18
    That book is strongly **not** recommended. Go for C++ Primer by Stan Lippman. – Prasoon Saurav Jan 26 '10 at 12:12
  • 23
    Well, there is your problem. Schildt is basically cr*p - get Accelerated C++ by Koenig & Moo. –  Jan 26 '10 at 12:12
  • 4
    Regarding "C++ Primer" vs. "Accelerated C++": The former is a heavy ~1000 pages, but explains everything and does so very detailed. The latter is only ~250 pages, but it has a very steep learning curve. Make your pick, both are very recommendable. – sbi Jan 26 '10 at 12:17
  • 10
    I wonder how nobody mentioned The C++ Programming Language by Bjarne Stroustrup. Bjarne Stroustrup is the creator of C++. A really good book to learn C++. – George Jan 26 '10 at 13:37
  • 15
    @George: TC++PL is not for beginners, but it is considered to be the _Bible_ for C++.xD – Prasoon Saurav Jan 26 '10 at 14:11
  • 4
    Thinking in C++ is more suited to beginners. And it's free: http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html – Alexandre C. Nov 13 '10 at 20:43
  • 1
    @Daniel: I really wonder why this one was protected. It's not like it did amass great amounts of "thanks!", "me too!", or spam answers or even comments. Or am I missing something? – sbi Mar 04 '11 at 08:27

8 Answers8

300

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. you must mutate the object, in which case, use pass by a non-const lvalue reference,
  3. 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 std::optional (boost::optional for older std libs), 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.

Community
  • 1
  • 1
sbi
  • 219,715
  • 46
  • 258
  • 445
  • 18
    +1 - I would also note that some (i.e. Google) feel that objects that will be changed within the function should be passed via a pointer instead of a non-const reference. The reasoning being that when the address of an object is passed to a function it is more apparent that said function may change it. Example: With references, the call is foo(bar); whether the reference is const or not, with a pointer it is foo(&bar); and is more apparent that foo is being passed a mutable object. – RC. Jan 26 '10 at 12:43
  • 20
    @RC Still doesn't tell you if the pointer is const or not. Google's guidelines have come in for a lot of flak in the various C++ online communities - justifiably so, IMHO. –  Jan 26 '10 at 12:48
  • 14
    While in other contexts google may be leading the way, in C++ their style guide is not really that good. – David Rodríguez - dribeas Jan 26 '10 at 14:51
  • 1
    @David Rodriguez - dribeas: Can you suggest some better C++ style guide? Preferably with a style checker? – Arun Oct 30 '10 at 02:04
  • 4
    @ArunSaha: As a pure style guide, Stroustrup has a [guide](http://www2.research.att.com/~bs/JSF-AV-rules.pdf) that was developed for an aerospace company. I browsed through the google guide and did not like it for a couple of reasons. Sutter & Alexandrescu [C++ Coding Standards](http://www.amazon.com/Coding-Standards-Rules-Guidelines-Practices/dp/0321113586) is a great book to read and you can get quite a few of good advices, but it is not really a *style guide*. I don't know of any automated checker for *style*, other than humans and common sense. – David Rodríguez - dribeas Oct 30 '10 at 11:46
  • 3
    @anon However, you do get a guarantee that if an argument is not passed via a pointer, then it is NOT changed. That's quite valuable IMHO, otherwise when trying to trace what happens to a variable in a function, you have to examine the header files of all the function's it's passed to to determine if it's changed. This way, you only have to look at the ones it was passed via pointer. – smehmood Mar 04 '11 at 07:20
  • And nobody mentioned aliasing?!!!? (Basically #4, but reversed. If objects can be changed outside the function, and the changes should not be visible inside, then pass by copy.) – Ben Voigt Jan 02 '12 at 23:03
  • @BenVoigt: That's only the case if you have some form of parallelism. "There's more to this, but these few beginner's rules will get you quite far." – sbi Oct 08 '12 at 11:39
  • @sbi: Recursion, signals, re-entrancy, callbacks all can result in the object being changed before the function completes. Are you categorizing all of those as "parallelism"? – Ben Voigt Oct 08 '12 at 13:51
  • @BenVoigt: No, of course not. You are right. Still, all those are not exactly beginner's problems (you really need to do what you're doing when you employ those), which is why I don't think it would be good to include this here. – sbi Oct 08 '12 at 14:00
  • @sbi: I can think of several beginner instances where callbacks occur (e.g. `std::sort` calling a user-provided comparison), but I haven't thought of any where that occurs *and* aliasing matters. – Ben Voigt Oct 08 '12 at 14:09
  • @sbi Your answer contains the wording "pass per X", "pass X" and "pass by X". Should I change them all to "pass by X", or is there a subtle difference between them that I missed? – anatolyg Dec 01 '13 at 13:26
  • @anatolyg: That's just me using English improperly, I guess. (Plus half of the answer was originally provided by DeadMG, so some of the discrepancy might come from there. Although I have to admit that I am perfectly capable of screwing up all on my own.) Please feel free to fix this. – sbi Dec 04 '13 at 08:17
  • Are there any performance gains or losses when passing by const reference? – Nubcake Aug 11 '17 at 17:58
  • @Nubcake: Compared to _what?_ – sbi Aug 18 '17 at 00:15
  • @sbi Oh sorry, I meant compared to passing by value or pointer. – Nubcake Aug 18 '17 at 00:41
  • @Nubcake: Passing by reference vs. pointer is a purely syntactical thing, the generated code might well be identical. Compared to passing by value, passing by reference prevents a copy of the object to be passed. Whether this makes a difference depends on what copying such an object costs. It's likely for more complex objects, hence the rule of thumb to pass built-ins by copy and UDTs by reference. – sbi Sep 06 '17 at 12:27
  • @sbi: update your answer with respect to C++14 & C++17 standard. – Destructor Mar 10 '18 at 06:28
  • @Destructor: (1) I'm not your employee. (2) The answer is CW. – sbi Mar 17 '18 at 21:00
  • If the object will only be moved, is const value ok? E.g. taking a const unique ptr – Brady Dean Apr 19 '18 at 04:11
  • @Brady Moved to or moved from? – sbi Apr 21 '18 at 05:07
  • Moved into a function. I tried it out with unique_ptr and it wouldn't work but I don't know why – Brady Dean Apr 21 '18 at 11:02
  • Well, given that you do not elaborate on what "wouldn't work", nobody else will be able to know why either. – sbi May 02 '18 at 17:42
  • *How* do you pass by value vs by reference? What does adding `&` mean? What about `*`? What about neither? – Aaron Franke Feb 27 '19 at 09:41
  • C++17 introduced `std::optional`; can be used instead of `boost::optional`. – legends2k Mar 01 '19 at 12:07
  • @legends2k: Thanks for the hint, I added that. (You could have changed that yourself, you know?) – sbi Mar 05 '19 at 20:26
  • Right, but since it's titled C++11, didn't wanna change a long-standing answer :) Thanks for heeding to my comment. – legends2k Mar 06 '19 at 02:07
119

There are some differences in calling conventions in C++ and Java. In C++ there are technically speaking only two conventions: pass-by-value and pass-by-reference, with some literature including a third pass-by-pointer convention (that is actually pass-by-value of a pointer type). On top of that, you can add const-ness to the type of the argument, enhancing the semantics.

Pass by reference

Passing by reference means that the function will conceptually receive your object instance and not a copy of it. The reference is conceptually an alias to the object that was used in the calling context, and cannot be null. All operations performed inside the function apply to the object outside the function. This convention is not available in Java or C.

Pass by value (and pass-by-pointer)

The compiler will generate a copy of the object in the calling context and use that copy inside the function. All operations performed inside the function are done to the copy, not the external element. This is the convention for primitive types in Java.

An special version of it is passing a pointer (address-of the object) into a function. The function receives the pointer, and any and all operations applied to the pointer itself are applied to the copy (pointer), on the other hand, operations applied to the dereferenced pointer will apply to the object instance at that memory location, so the function can have side effects. The effect of using pass-by-value of a pointer to the object will allow the internal function to modify external values, as with pass-by-reference and will also allow for optional values (pass a null pointer).

This is the convention used in C when a function needs to modify an external variable, and the convention used in Java with reference types: the reference is copied, but the referred object is the same: changes to the reference/pointer are not visible outside the function, but changes to the pointed memory are.

Adding const to the equation

In C++ you can assign constant-ness to objects when defining variables, pointers and references at different levels. You can declare a variable to be constant, you can declare a reference to a constant instance, and you can define all pointers to constant objects, constant pointers to mutable objects and constant pointers to constant elements. Conversely in Java you can only define one level of constant-ness (final keyword): that of the variable (instance for primitive types, reference for reference types), but you cannot define a reference to an immutable element (unless the class itself is immutable).

This is extensively used in C++ calling conventions. When the objects are small you can pass the object by value. The compiler will generate a copy, but that copy is not an expensive operation. For any other type, if the function will not change the object, you can pass a reference to a constant instance (usually called constant reference) of the type. This will not copy the object, but pass it into the function. But at the same time the compiler will guarantee that the object is not changed inside the function.

Rules of thumb

This are some basic rules to follow:

  • Prefer pass-by-value for primitive types
  • Prefer pass-by-reference with references to constant for other types
  • If the function needs to modify the argument use pass-by-reference
  • If the argument is optional, use pass-by-pointer (to constant if the optional value should not be modified)

There are other small deviations from these rules, the first of which is handling ownership of an object. When an object is dynamically allocated with new, it must be deallocated with delete (or the [] versions thereof). The object or function that is responsible for the destruction of the object is considered the owner of the resource. When a dynamically allocated object is created in a piece of code, but the ownership is transfered to a different element it is usually done with pass-by-pointer semantics, or if possible with smart pointers.

Side note

It is important to insist in the importance of the difference between C++ and Java references. In C++ references are conceptually the instance of the object, not an accessor to it. The simplest example is implementing a swap function:

// C++
class Type; // defined somewhere before, with the appropriate operations
void swap( Type & a, Type & b ) {
   Type tmp = a;
   a = b;
   b = tmp;
}
int main() {
   Type a, b;
   Type old_a = a, old_b = b;
   swap( a, b );
   assert( a == old_b );
   assert( b == old_a ); 
}

The swap function above changes both its arguments through the use of references. The closest code in Java:

public class C {
   // ...
   public static void swap( C a, C b ) {
      C tmp = a;
      a = b;
      b = tmp;
   }
   public static void main( String args[] ) {
      C a = new C();
      C b = new C();
      C old_a = a;
      C old_b = b;
      swap( a, b ); 
      // a and b remain unchanged a==old_a, and b==old_b
   }
}

The Java version of the code will modify the copies of the references internally, but will not modify the actual objects externally. Java references are C pointers without pointer arithmetic that get passed by value into functions.

sbi
  • 219,715
  • 46
  • 258
  • 445
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
24

Pass by value:

void func (vector v)

Pass variables by value when the function needs complete isolation from the environment i.e. to prevent the function from modifying the original variable as well as to prevent other threads from modifying its value while the function is being executed.

The downside is the CPU cycles and extra memory spent to copy the object.

Pass by const reference:

void func (const vector& v);

This form emulates pass-by-value behavior while removing the copying overhead. The function gets read access to the original object, but cannot modify its value.

The downside is thread safety: any change made to the original object by another thread will show up inside the function while it's still executing.

Pass by non-const reference:

void func (vector& v)

Use this when the function has to write back some value to the variable, which will ultimately get used by the caller.

Just like the const reference case, this is not thread-safe.

Pass by const pointer:

void func (const vector* vp);

Functionally same as pass by const-reference except for the different syntax, plus the fact that the calling function can pass NULL pointer to indicate it has no valid data to pass.

Not thread-safe.

Pass by non-const pointer:

void func (vector* vp);

Similar to non-const reference. The caller typically sets the variable to NULL when the function is not supposed to write back a value. This convention is seen in many glibc APIs. Example:

void func (string* str, /* ... */) {
    if (str != NULL) {
        *str = some_value; // assign to *str only if it's non-null
    }
}

Just like all pass by reference/pointer, not thread-safe.

nav
  • 1,645
  • 15
  • 22
24

There are several cases to consider.

Parameter modified ("out" and "in/out" parameters)

void modifies(T &param);
// vs
void modifies(T *param);

This case is mostly about style: do you want the code to look like call(obj) or call(&obj)? However, there are two points where the difference matters: the optional case, below, and you want to use a reference when overloading operators.

...and optional

void modifies(T *param=0);  // default value optional, too
// vs
void modifies();
void modifies(T &param);

Parameter not modified

void uses(T const &param);
// vs
void uses(T param);

This is the interesting case. The rule of thumb is "cheap to copy" types are passed by value — these are generally small types (but not always) — while others are passed by const ref. However, if you need to make a copy within your function regardless, you should pass by value. (Yes, this exposes a bit of implementation detail. C'est le C++.)

...and optional

void uses(T const *param=0);  // default value optional, too
// vs
void uses();
void uses(T const &param);  // or optional(T param)

There's the least difference here between all situations, so choose whichever makes your life easiest.

Const by value is an implementation detail

void f(T);
void f(T const);

These declarations are actually the exact same function! When passing by value, const is purely an implementation detail. Try it out:

void f(int);
void f(int const) { /* implements above function, not an overload */ }

typedef void NC(int);       // typedefing function types
typedef void C(int const);

NC *nc = &f;  // nc is a function pointer
C *c = nc;    // C and NC are identical types
0

Since no one mentioned I am adding on it, When you pass a object to a function in c++ the default copy constructor of the object is called if you dont have one which creates a clone of the object and then pass it to the method, so when you change the object values that will reflect on the copy of the object instead of the original object, that is the problem in c++, So if you make all the class attributes to be pointers, then the copy constructors will copy the addresses of the pointer attributes , so when the method invocations on the object which manipulates the values stored in pointer attributes addresses, the changes also reflect in the original object which is passed as a parameter, so this can behave same a Java but dont forget that all your class attributes must be pointers, also you should change the values of pointers, will be much clear with code explanation.

Class CPlusPlusJavaFunctionality {
    public:
       CPlusPlusJavaFunctionality(){
         attribute = new int;
         *attribute = value;
       }

       void setValue(int value){
           *attribute = value;
       }

       void getValue(){
          return *attribute;
       }

       ~ CPlusPlusJavaFuncitonality(){
          delete(attribute);
       }

    private:
       int *attribute;
}

void changeObjectAttribute(CPlusPlusJavaFunctionality obj, int value){
   int* prt = obj.attribute;
   *ptr = value;
}

int main(){

   CPlusPlusJavaFunctionality obj;

   obj.setValue(10);

   cout<< obj.getValue();  //output: 10

   changeObjectAttribute(obj, 15);

   cout<< obj.getValue();  //output: 15
}

But this is not good idea as you will be ending up writing lot of code involving with pointers, which are prone for memory leaks and do not forget to call destructors. And to avoid this c++ have copy constructors where you will create new memory when the objects containing pointers are passed to function arguments which will stop manipulating other objects data, Java does pass by value and value is reference, so it do not require copy constructors.

murali kurapati
  • 1,510
  • 18
  • 23
0

Do I need to pass pointers, references, or non-pointer and non-reference values?

This is a question that matters when writing a function and choosing the types of the parameters it takes. That choice will affect how the function is called and it depends on a few things.

The simplest option is to pass objects by value. This basically creates a copy of the object in the function, which has many advantages. But sometimes copying is costly, in which case a constant reference, const&, is usually best. And sometimes you need your object to be changed by the function. Then a non-constant reference, &, is needed.

For guidance on the choice of parameter types, see the Functions section of the C++ Core Guidelines, starting with F.15. As a general rule, try to avoid raw pointers, *.

John McFarlane
  • 5,528
  • 4
  • 34
  • 38
-1

There are three methods of passing an object to a function as a parameter:

  1. Pass by reference
  2. pass by value
  3. adding constant in parameter

Go through the following example:

class Sample
{
public:
    int *ptr;
    int mVar;

    Sample(int i)
    {
        mVar = 4;
        ptr = new int(i);
    }

    ~Sample()
    {
        delete ptr;
    }

    void PrintVal()
    {
        cout << "The value of the pointer is " << *ptr << endl
             << "The value of the variable is " << mVar;
   }
};

void SomeFunc(Sample x)
{
cout << "Say i am in someFunc " << endl;
}


int main()
{

  Sample s1= 10;
  SomeFunc(s1);
  s1.PrintVal();
  char ch;
  cin >> ch;
}

Output:

Say i am in someFunc
The value of the pointer is -17891602
The value of the variable is 4

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
amerr
  • 95
  • 1
  • 2
  • 8
  • There's only 2 methods (the first 2 you mentioned). No idea what you meant by "passing constant in parameter" – M.M Oct 31 '16 at 03:03
-2

The following are the ways to pass a arguments/parameters to function in C++.

1. by value.

// passing parameters by value . . .

void foo(int x) 
{
    x = 6;  
}

2. by reference.

// passing parameters by reference . . .

void foo(const int &x) // x is a const reference
{
    x = 6;  
}

// passing parameters by const reference . . .

void foo(const int &x) // x is a const reference
{
    x = 6;  // compile error: a const reference cannot have its value changed!
}

3. by object.

class abc
{
    display()
    {
        cout<<"Class abc";
    }
}


// pass object by value
void show(abc S)
{
    cout<<S.display();
}

// pass object by reference
void show(abc& S)
{
    cout<<S.display();
}
Yogeesh H T
  • 2,777
  • 21
  • 18
  • 2
    "pass by object" isn't a thing. There is only pass by value, and pass by reference. Your "case 3" actually shows one case of pass by value and one case of pass by reference. – M.M Oct 31 '16 at 03:02