Difference between to...

What is the difference between to pass by reference, to pass by address and to pass by value?
Google?

Pass By Reference the variable address is passed to a function. Whatever changes made to the formal parameter will affect to the actual parameters cause it's the same memory location used for both variables. This is useful when you required to return more then 1 values.

Pass By Value the value of the variable is passed. Changes made to formal will not affect the actual parameters.

Hope this helps...
Last edited on
Thanks man...I already know these(from this site's tutorial)...but from a different source, I saw "there are two ways to pass arguments. By reference and by address." I was thinking they meant reference and value, but I want to be sure that's why I'm posting it here.

Anyway, thanks for replying!
> I saw "there are two ways to pass arguments. By reference and by address."
> I was thinking they meant reference and value

You've got it right; there are only these two ways - by reference or by value.

What is passed by value or by reference may be any object, including a pointer. ( C++11: The reference may be either an lvalue reference or an rvalue reference.)

1
2
3
4
void foo( int ) ; // pass int by value
void foo( int* ) ; // pass pointer by value
void bar( int& ) ; // pass int by reference
void bar( int*& ) ; // pass pointer by reference 
@JLBorges

Thanks man! but how do I pass the reference by an lvalue or an rvalue?
closed account (zb0S216C)
@Aceix: Do you mean passing by L-Value, or R-Value reference?

1
2
3
4
5
6
7
8
// L-Value reference:
void function(int &);

// R-Value reference (pre-C++11):
void function(int const &);

// R-Value reference (C++11):
void function(int &&);

Wazzak
> how do I pass the reference by an lvalue or an rvalue?

Perhaps you should not be bothering about this right now, except for taking it for granted that if we compile this code with a C++11 compiler:

1
2
3
4
5
6
7
8
9
10
11
12
13
std::vector< std::string > make_vec() 
{
     std::std::vector< std::string > result ;
     result.emplace_back( "abcd" ) ;
     result.emplace_back( "efg" ) ;
     result.emplace_back( "hijklm" ) ;
     return result ;
}

int main()
{
    std::vector< std::string > myvec = make_vec() ;
}


no copy of the contents of a vector or even a string would be made - standard library containers are moveable.

A good explanation is available here:
http://thbecker.net/articles/rvalue_references/section_01.html

and also in this series of articles:
http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/

Have a glance at those, and go back to them later when you are ready.

After having had a first look, you could run this program and try to explain the output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <vector>

void foo( std::vector<int>& seq, const char* expr_string )
{ std::cout << "foo - " << expr_string << " passed by lvalue reference\n" ; }

void foo( const std::vector<int>& seq, const char* expr_string )
{ std::cout << "foo - " << expr_string << " passed by lvalue reference to const\n" ; }

void foo( std::vector<int>&& seq, const char* expr_string )
{ std::cout << "foo - " << expr_string << " passed by rvalue reference\n" ; }


void bar( std::vector<int>& seq, const char* expr_string )
{ std::cout << "bar - " << expr_string << " passed by lvalue reference\n" ; }

void bar( const std::vector<int>& seq, const char* expr_string )
{ std::cout << "bar - " << expr_string << " passed by lvalue reference to const\n" ; }


void baz( const std::vector<int>& seq, const char* expr_string )
{ std::cout << "baz - " << expr_string << " passed by lvalue reference to const\n" ; }


std::vector<int> rvalue_expr() { return { 0, 1, 2, 3, 4, 5 } ; }

#define FOO(a) foo( (a), #a )
#define BAR(a) bar( (a), #a )
#define BAZ(a) baz( (a), #a )

int main ()
{
    std::vector<int> lvalue_expr = { 6, 7, 8, 9 } ;
    const std::vector<int> const_lvalue_expr = rvalue_expr() ;
    FOO( lvalue_expr ) ;
    FOO( const_lvalue_expr ) ;
    FOO( rvalue_expr() ) ;
    std::cout << "--------------\n" ;

    BAR( lvalue_expr ) ;
    BAR( const_lvalue_expr ) ;
    BAR( rvalue_expr() ) ;
    std::cout << "--------------\n" ;

    BAZ( lvalue_expr ) ;
    BAZ( const_lvalue_expr ) ;
    BAZ( rvalue_expr() ) ;
}

Last edited on
Topic archived. No new replies allowed.