pointers anbd references as parameters



 DEVELOP > c-Plus-Plus > pointers anbd references as parameters

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "E.T. Grey"
Date: 02 Sep 2006 12:29:31 PM
Object: pointers anbd references as parameters
Can I pass a pointer instead of a reference (and vice versa)? - I know a
reference and a pointer are not the same - but i can't help feeling I
can "get away with this".
Bad practise, ok practise, or must be avoided at all times?
If must be avoided, can I hust take the address of a reference and use
that as a pointer? (actually, its mre useful if I can pass a pointer as
reference
PS: Alarm bells ringing though - a ptr can be null, whereas a reference
can't be .. Hmmmm, looks like "terra infestus" (dangerous ground) to me ...
.

User: "Jim Langston"

Title: Re: pointers anbd references as parameters 01 Sep 2006 05:37:04 PM
"E.T. Grey" <nebula@alpha-centauri.com> wrote in message
news:YNGdnRElsP-4ImTZRVnyjw@bt.com...

Can I pass a pointer instead of a reference (and vice versa)? - I know a
reference and a pointer are not the same - but i can't help feeling I can
"get away with this".

It depends on where you want to pass it. If a function or method expects a
reference then you couldn't pass a pointer to it, but you could modify the
function/method to accept a pointer instead.

Bad practise, ok practise, or must be avoided at all times?

Useful practice in some instances.

If must be avoided, can I hust take the address of a reference and use
that as a pointer? (actually, its mre useful if I can pass a pointer as
reference

Hopefully this code shows you what you are asking about.
#include <string>
#include <iostream>
void MyRefF( int& IntR )
{
// How to use IntR as a pointer? This works for me.
int* IntP = &IntR;
*IntP = 20;
std::cout << *IntP << std::endl;
}
int main()
{
int* MyIntP = new int(10);
// So how do we pass MyIntP to MyRefF? Dereference it.
MyRefF( *MyIntP );
std::cout << *MyIntP << std::endl;
delete MyIntP;
MyIntP = NULL;
// Following is undefined behavior, and gives me illegal memory access
// when the variable is used inside of MyRefF
MyRefF( *MyIntP );
std::string wait;
std::getline( std::cin, wait );
}

PS: Alarm bells ringing though - a ptr can be null, whereas a reference
can't be .. Hmmmm, looks like "terra infestus" (dangerous ground) to me
...

And you are right to think that, as the code shows.
.

User: "Alf P. Steinbach"

Title: Re: pointers anbd references as parameters 02 Sep 2006 12:32:22 PM
* E.T. Grey:


PS: Alarm bells ringing though - a ptr can be null, whereas a reference
can't be .. Hmmmm, looks like "terra infestus" (dangerous ground) to me ...

Yes, that's why you preferentially should use references for arguments,
and pointers only where null is a possibility you actively support. One
common exception is to pass pointers for dynamically allocated objects.
A probably much better practice is to use smart pointers for those.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.
User: "Frederick Gotham"

Title: Re: pointers anbd references as parameters 03 Sep 2006 12:51:28 PM
Alf P. Steinbach posted:

* E.T. Grey:


PS: Alarm bells ringing though - a ptr can be null, whereas a reference
can't be .. Hmmmm, looks like "terra infestus" (dangerous ground) to me
...


Yes, that's why you preferentially should use references for arguments,
and pointers only where null is a possibility you actively support.

Another common usage of passing pointers by value is where you are dealing
with an array, e.g.:
void Func(MyClass *p,size_t len)
{
assert(p); assert(len);
do
{
/* Manipulate the array */
} while(--len);
}
--
Frederick Gotham
.
User: "Alf P. Steinbach"

Title: Re: pointers anbd references as parameters 03 Sep 2006 01:23:28 PM
* Frederick Gotham:

Alf P. Steinbach posted:

* E.T. Grey:

PS: Alarm bells ringing though - a ptr can be null, whereas a reference
can't be .. Hmmmm, looks like "terra infestus" (dangerous ground) to me
...

Yes, that's why you preferentially should use references for arguments,
and pointers only where null is a possibility you actively support.



Another common usage of passing pointers by value is where you are dealing
with an array, e.g.:

void Func(MyClass *p,size_t len)
{
assert(p); assert(len);

do
{
/* Manipulate the array */
} while(--len);
}

Use
void func( std::vector<MyClass> const& v )
{
for( ... )
{
...
}
}
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.
User: "Frederick Gotham"

Title: Re: pointers anbd references as parameters 03 Sep 2006 01:32:09 PM
Alf P. Steinbach posted:

Use

void func( std::vector<MyClass> const& v )
{
for( ... )
{
...
}
}

I prefer to use arrays for simple tasks. I would find a vector more
appropriate if the code were a little more complicated.
--
Frederick Gotham
.
User: "Thomas J. Gritzan"

Title: Re: pointers anbd references as parameters 03 Sep 2006 01:49:12 PM
Frederick Gotham schrieb:

Alf P. Steinbach posted:

Use

void func( std::vector<MyClass> const& v )
{
for( ... )
{
...
}
}



I prefer to use arrays for simple tasks. I would find a vector more
appropriate if the code were a little more complicated.

I started to use std::vector everytime I would use new[].
Why? Because it's easier and it makes short & clear code. The class holds
the actual size, and deletes the memory in case of exception and normal
cleanup.
The opposite is in my (windows) driver code, where I write more "C-like".
The code looks like this:
<bad code>
Obj* o1 = CreateSomeObject();
if (!o1)
{
// handle error
return;
}
Obj* o2 = CreateSomeOtherObject();
if (!o2)
{
// handle error
free(o1); // don't forget it!
return;
}
Obj* o3 = CreateSomeOtherObject();
if (!o3)
{
// handle error
free(o2);
free(o1); // don't forget one of it!
return;
}
free(o3);
free(o2);
free(o1);
</bad code>
Perhaps I should start using scope guards in kernel code also.
--
Thomas
http://www.netmeister.org/news/learn2quote.html
.





User: "Frederick Gotham"

Title: Re: pointers anbd references as parameters 03 Sep 2006 12:49:48 PM
E.T. Grey posted:

Can I pass a pointer instead of a reference (and vice versa)?

Depends on the context. In general: Yes.

- I know a reference and a pointer are not the same - but i can't help
feeling I can "get away with this".

Pointers are different in that they are an actual object.
A reference is simply a idea, a mystical concept.

Bad practise, ok practise, or must be avoided at all times?

A lot of people like references; they're handier to use.

If must be avoided, can I hust take the address of a reference and use
that as a pointer?

A reference isn't an object (nor a function) -- it doesn't have an address.
Once a reference has been declared, any usage of it is actually usage of
the object which the reference refers to. For example:
int i = 5;
int &r = i;
Func(&r); /* This passes the address of i */

(actually, its mre useful if I can pass a pointer as reference

Depends on context.
--
Frederick Gotham
.
User: "Diego Martins"

Title: Re: pointers anbd references as parameters 04 Sep 2006 11:45:34 AM
Frederick Gotham wrote:

E.T. Grey posted:

Can I pass a pointer instead of a reference (and vice versa)?




Depends on the context. In general: Yes.

when you need a SENTINEL, you can't use references
I use const pointers, in this case
Diego
HP
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER