473,472 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

By reference or by pointer

What is the difference between passing argument by reference or by pointer?
Is there any difference in terms of PERFORMANCE ?
When and why one of these method (by reference or by pointer) should be
preferred?

THX.
typedef struct{
int X,Y,Z}TMyStructure;

void Function1(TMyStructure &xyz)
{
xyz.X = 123;
.. . .
}

void Function2(TMyStructure *xyz)
{
xyz->X = 123;
.. . .
}
Jul 22 '05 #1
5 3428
<cut> writes:
What is the difference between passing argument by reference or by pointer?

See
http://www.parashift.com/c++-faq-lite/references.html
Jul 22 '05 #2
typedef struct{
int X,Y,Z}TMyStructure;

void Function1(TMyStructure &xyz)
{
xyz.X = 123;
. . .
}

void Function2(TMyStructure *xyz)
{
xyz->X = 123;
. . .
}

It's a question of style. I use references for input, pointers for
output, but do as you like.
-Gernot
Jul 22 '05 #3

<cut> wrote in message
news:41***********************@news.optusnet.com.a u...
What is the difference between passing argument by reference or by pointer? Is there any difference in terms of PERFORMANCE ?


If you use a reference then the compiler has a slightly easier job of
optimising your code. This is because it knows that a reference will always
refer to the same object, but a pointer can be changed to point somewhere
else.

john
Jul 22 '05 #4

<cut> skrev i en meddelelse
news:41***********************@news.optusnet.com.a u...
What is the difference between passing argument by reference or by
pointer?
Is there any difference in terms of PERFORMANCE ?
When and why one of these method (by reference or by pointer) should be
preferred?

THX.
typedef struct{
int X,Y,Z}TMyStructure;

void Function1(TMyStructure &xyz)
{
xyz.X = 123;
. . .
}

void Function2(TMyStructure *xyz)
{
xyz->X = 123;
. . .
}


Use a reference whenever possible, and a pointer (perhaps a smart one) only
if you can not. If the above code is correct, you should have used a
reference. Change it as below and a pointer is correct.

void Function2(TMyStructure *xyz)
{
if (xyz)
{
. . .
}
}
Jul 22 '05 #5

<cut> wrote in message
news:41***********************@news.optusnet.com.a u...
What is the difference between passing argument by reference or by pointer? Is there any difference in terms of PERFORMANCE ?
When and why one of these method (by reference or by pointer) should be
preferred?

THX.
typedef struct{
int X,Y,Z}TMyStructure;

void Function1(TMyStructure &xyz)
{
xyz.X = 123;
. . .
}

void Function2(TMyStructure *xyz)
{
xyz->X = 123;
. . .
}


In C, function parameters were not allowed to be passed in by reference
(using &). The feature was added for C++ to make code easier to read. So
performance wise, I beleive they should be the same.
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
9
by: Sandy | last post by:
Hi, In one of my interview I was asked a question, whether using pointers for argument is efficient then reference or not. i.e. void fun(Complex *p) void fun(Complex &ref) can somebody...
18
by: man | last post by:
can any one please tell me what is the diff between pointer and reference.....and which one is better to use ....and why???????
12
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
13
by: al.cpwn | last post by:
I get that these two are different int* get() { static int m; return &m; } int& get() {
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
8
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference...
11
by: asdf | last post by:
C++ allows a reference to a pointer, but doesn't allow a pointer to a reference, why?
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
41
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.