By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,141 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,141 IT Pros & Developers. It's quick & easy.

pass by-reference

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a

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

P: n/a
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.