471,573 Members | 1,675 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,573 software developers and data experts.

pass by-reference

Hi, how really work behind the scene the pass-by-reference in C++?

I know that in C++ like the C the objects are passed by-values and if
we want
we can simulate the pass-by-reference using the pointers...

Bye

Oct 9 '06 #1
5 2136
josh wrote:
Hi, how really work behind the scene the pass-by-reference in C++?
Well, you pass a reference, like:

void function(MyClass& object);
I know that in C++ like the C the objects are passed by-values and if
we want we can simulate the pass-by-reference using the pointers...
In C++, we can also pass a reference directly.

Oct 9 '06 #2
"josh" <xd********@yahoo.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
Hi, how really work behind the scene the pass-by-reference in C++?

I know that in C++ like the C the objects are passed by-values and if
we want
we can simulate the pass-by-reference using the pointers...

Bye
I'm not quite sure what your quesiton is, is it how do we work with passing
by reference, or how does the compiler do it itself?

How we do it is quite simple:

void MyFunc( std::string& MyString )
{
std::cout << "Length " << MyString.length() << std::endl;
MyString = "";
}

int main()
{
std::string SomeString = "Hello";
MyFunc( SomeString );
}

In a function/method that accepts a reference you can use the variable as if
it was declared on the local scope, yet it effects the passed in varaible.
That is, in this example, SomeString is actually being cleared because the
reference to it is being cleared in the function.

How does it work behind the scenes? As I understand it, the compiler
actually just passes some type of pointer (some just a pointer, some maybe
more), and the compiler treats it as a reference instead of a pointer, I.E.
I used .length() and not ->length().

There are more complexities of variables being delared as references, such
as they have to be initialized (seated) and you can not reseat a reference.
In this case they just become aliases for a variable.

int main()
{
int MyInt = 10;
int& MyIntRef = MyInt;
// Above line makes MyIntRef point to MyInt

MyIntRef = 20;
// MyInt is now 20
}

Does that help?
Oct 9 '06 #3
Yes, thanks... I was trying to use it as if I was returning a pointer.
Ofcause I can't :)

Thanks alot.
>
Does that help?

Oct 10 '06 #4

Jim Langston ha scritto:
How does it work behind the scenes? As I understand it, the compiler
actually just passes some type of pointer (some just a pointer, some maybe
more), and the compiler treats it as a reference instead of a pointer, I.E.
I used .length() and not ->length().
so it means that a pointer argument is different from a reference
argument
bacause:
- when an argument is a pointer it has an address of a variable who
is pointing to and we can modify its value only indirectly with
deference operation
and that address is passed by-value
- when an argument is a reference it is like the variable passed and
when we modify
it we modify directly the variable passed by-reference

If I want to write a swapping function (with by-reference) will be:

int a=10,b=100
void swap(int &a, int &b)
{
int temp = 0;
int &tmp = temp;

tmp = a;
a = b;
b = tmp;
}
swap(a, b); // a will be 100 and b will be 10
cout << a << " " << b << endl;

but with pointer (passed by-value)
void swapP(int *a, int *b)
{
int tmp;

tmp = *a;
*a = *b;
*b = tmp;
}

here we can directly swap the variables but we can do that only with
the values.....
Does that help?
yes

Oct 10 '06 #5
josh schrieb:
so it means that a pointer argument is different from a reference
argument
bacause:
- when an argument is a pointer it has an address of a variable who
is pointing to and we can modify its value only indirectly with
deference operation
and that address is passed by-value
yyyes..
- when an argument is a reference it is like the variable passed and
when we modify
it we modify directly the variable passed by-reference
yes.
If I want to write a swapping function (with by-reference) will be:

int a=10,b=100
void swap(int &a, int &b)
{
int temp = 0;
int &tmp = temp;
Why do you use two variables here?
Just use one variable as in the pointer swap function:

int tmp;
tmp = a;
a = b;
b = tmp;
}
swap(a, b); // a will be 100 and b will be 10
cout << a << " " << b << endl;

but with pointer (passed by-value)
void swapP(int *a, int *b)
{
int tmp;

tmp = *a;
*a = *b;
*b = tmp;
}
A reference is just another name for an object:

int i = 5; // i is an object of type int, storing value 5.
int &ri = i; // ri is another name for i.

You can use "i" or "ri" after that to access the same object.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Oct 10 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Zlatko Matić | last post: by
8 posts views Thread by Blue Ocean | last post: by
1 post views Thread by Greg Strong | last post: by
1 post views Thread by Hexman | last post: by
31 posts views Thread by Sam of California | last post: by
3 posts views Thread by Vols | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.