473,396 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 2295
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Zlatko Matić | last post by:
Hello. I'm wondernig what is happennig whith saved pass-through queries nested in regular JET query if regular JET query just filtrates result by start/end date...Does pass-through query first...
0
by: Zlatko Matić | last post by:
Hi everybody! Recently I was struggling with client/server issues in MS Access/PostgreSQL combination. Although Access is intuitive and easy to use desktop database solution, many problems...
8
by: Blue Ocean | last post by:
I know this is somewhat dependent on the circumstances, but let me ask anyway. Suppose I have a 100 byte struct or array or something like that. Which would be more efficient? void...
1
by: Greg Strong | last post by:
Hello All, Why would brackets be added to the SQL of a pass through query to Oracle? If I paste the debug print of the SQL statement into SQLPlus of Oracle's XE edition it works, and does NOT...
1
by: Hexman | last post by:
Hello Again, I have a table that contains data that needs to be ranked. There are actually 2 columns that need to be ranked. I have already completed the task but did it in a "Brute Force" way....
3
by: QQ | last post by:
I have one integer array int A; I need to pass this array into a function and evaluate this array in this function how should I pass? Is it fine? void test(int *a)
31
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes...
9
by: mosullivan | last post by:
I can't figure out how to print after every pass through the bubble sort. I'm supposed to display the sort after every pass through the loop. Below is what I have so far. #include <stdio.h>...
3
by: Vols | last post by:
void f (char *a) { a++; } int main (void) { char a = "abc";// or char *a = "abc"; f (a); puts (a);
13
by: magickarle | last post by:
Hi, I got a pass-through query (that takes about 15 mins to process) I would like to integrate variables to it. IE: something simple: Select EmplID from empl_Lst where empl_lst.timestamp between...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.