473,473 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

reference or pointer in parameter list - which is prefereable?


i have two functions and both of them are to make changes into object field or get some results into caller function:

void FooFunc1(FooClass& a)
{ ...

void FooFunc2(FooClass* a)
{ ...

or

void FooFunc3(int& a)
{ ...

void FooFunc4(int* a)
{ ...
in what cases it is prefereble to use reference or pointer in parameter list?

many thanks.
May 3 '06 #1
6 1904
Maksim Kasimov wrote:

i have two functions and both of them are to make changes into object
field or get some results into caller function:

void FooFunc1(FooClass& a)
{ ...

void FooFunc2(FooClass* a)
{ ...

or

void FooFunc3(int& a)
{ ...

void FooFunc4(int* a)
{ ...
in what cases it is prefereble to use reference or pointer in parameter
list?

I'd say neither. I'd use a smart pointer type, but that's my style.

There are may arguments for and against both, look back through the
group archives.

--
Ian Collins.
May 3 '06 #2
On Wed, 03 May 2006 21:41:34 +1200, Ian Collins <ia******@hotmail.com>
wrote:
Maksim Kasimov wrote:
in what cases it is prefereble to use reference or pointer in parameter
list?

I'd say neither. I'd use a smart pointer type, but that's my style.


"Smart" pointers should _never_ be used as function parameters!

Best wishes,
Roland Pibinger
May 3 '06 #3
On Wed, 03 May 2006 12:32:45 +0300, Maksim Kasimov
<ma************@gmail.com> wrote:

i have two functions and both of them are to make changes into object field or get some results into caller function:

void FooFunc1(FooClass& a)
{ ...

void FooFunc2(FooClass* a)
{ ...

or

void FooFunc3(int& a)
{ ...

void FooFunc4(int* a)
{ ...
in what cases it is prefereble to use reference or pointer in parameter list?


Use references when you can pointers when you must (IIRC, this is a
FAQ). Pointers are only necessary when you also need to pass 0 (NULL)
to the function. The latter probably indicates a design problem
anyway.

Best wishes,
Roland Pibinger
May 3 '06 #4
Roland Pibinger wrote:
On Wed, 03 May 2006 21:41:34 +1200, Ian Collins <ia******@hotmail.com>
wrote:
Maksim Kasimov wrote:
in what cases it is prefereble to use reference or pointer in parameter
list?


I'd say neither. I'd use a smart pointer type, but that's my style.

"Smart" pointers should _never_ be used as function parameters!

Why?

--
Ian Collins.
May 3 '06 #5

Ian Collins wrote:
Maksim Kasimov wrote:
void FooFunc3(int& a)
{ ...

void FooFunc4(int* a)
{ ...
in what cases it is prefereble to use reference or pointer in parameter
list?

I'd say neither. I'd use a smart pointer type, but that's my style.


What if the parameter isn't dynamically allocated?

May 4 '06 #6
Squeamizh wrote:
Ian Collins wrote:
Maksim Kasimov wrote:
void FooFunc3(int& a)
{ ...

void FooFunc4(int* a)
{ ...
in what cases it is prefereble to use reference or pointer in parameter
list?


I'd say neither. I'd use a smart pointer type, but that's my style.

What if the parameter isn't dynamically allocated?

Then you can't pass it.

I guess I've been working in an environment where every object is some
form of smart pointer, I'd overlooked the obvious!

I tend to faviour references in the general case.

--
Ian Collins.
May 4 '06 #7

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...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
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...
10
by: ravi | last post by:
Hi, i am a c++ programmer, now i want to learn programming in c also. so can anybody explain me the difference b/w call by reference and call by pointer (with example if possible).
13
by: 1337-chixor;) | last post by:
Hi guys, I'm dealing with, probably, easy problem, however I'm not sure what to do and whether my way of thinking is correct.. The task is to implement a list of object of a class ListOfElements...
7
by: arnuld | last post by:
this programme gives unusual output. i am not able to find out where the semantic bug lies: /* C++ Primer - 4/e * * an example from section section 7.2.4, page 241 * STATEMENT * write a...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
6
by: Karthik V | last post by:
I've been facing this situation often: class NewFeature { TheClass *theClass; NewFeature(TheClass *t) { theClass = t; } void act() { // call methods on theClass } }
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
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 ...

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.