Connecting Tech Pros Worldwide Help | Site Map

Pass by Pointer * or Pass by Reference &?

 
LinkBack Thread Tools Search this Thread
  #1  
Old August 19th, 2005, 11:55 AM
Robert
Guest
 
Posts: n/a
Default Pass by Pointer * or Pass by Reference &?

Hello all,

I am a C programmer learning C++. And I am confused with function Pass
by Pointer * or Pass by Reference &.

Function pass by pointer like:
void func(int * x)
And function pass by referencelike:
void func(int & x)

What's their main difference and be used in what circumstance?

Best regards,
Robert


  #2  
Old August 19th, 2005, 12:05 PM
Christian Meier
Guest
 
Posts: n/a
Default Re: Pass by Pointer * or Pass by Reference &?

"Robert" <zhushenli@gmail.com> schrieb im Newsbeitrag
news:1124451988.918205.34840@g14g2000cwa.googlegro ups.com...[color=blue]
> Hello all,
>
> I am a C programmer learning C++. And I am confused with function Pass
> by Pointer * or Pass by Reference &.
>
> Function pass by pointer like:
> void func(int * x)
> And function pass by referencelike:
> void func(int & x)
>
> What's their main difference and be used in what circumstance?
>
> Best regards,
> Robert
>[/color]
Pointer can be null, references can't. Pointers can be reassigned.
A reference is more comfortable to use. You can handle it like a normal
object. When changing a function from
void func(const AClass);
to
void func(const AClass&);
you do not need to rewrite your code.

Greetings Chris


  #3  
Old August 19th, 2005, 12:05 PM
John Ratliff
Guest
 
Posts: n/a
Default Re: Pass by Pointer * or Pass by Reference &?

Robert wrote:[color=blue]
> Hello all,
>
> I am a C programmer learning C++. And I am confused with function Pass
> by Pointer * or Pass by Reference &.
>
> Function pass by pointer like:
> void func(int * x)
> And function pass by referencelike:
> void func(int & x)
>
> What's their main difference and be used in what circumstance?
>
> Best regards,
> Robert
>[/color]

There is a lot of good information about this in this handy guide:

http://www.parashift.com/c++-faq-lite/

--John Ratliff
  #4  
Old August 19th, 2005, 12:15 PM
Robert
Guest
 
Posts: n/a
Default Re: Pass by Pointer * or Pass by Reference &?

Another problem is why reference cannot be able to represent a NULL
object?

Best regards,
Robert

  #5  
Old August 20th, 2005, 08:55 PM
Frank Chang
Guest
 
Posts: n/a
Default Re: Pass by Pointer * or Pass by Reference &?

One other difference between pointers and references is that if you use
dynamic_cast on an incorrect reference it willl throw a bad cast
exception. On the other hand, an incorrect dynamic cast on a pointer
will not throw a bad cast exception.

void function(Panda& q)
{
Panda &ip = dynamic_cast<randomref&)(q);
}

void g()
{
try{
f(*(new Panda))
}
catch(bad_cast)
{

}
}

  #6  
Old August 22nd, 2005, 02:15 PM
Giulio Guarnone
Guest
 
Posts: n/a
Default Re: Pass by Pointer * or Pass by Reference &?

Robert ha scritto:[color=blue]
> Another problem is why reference cannot be able to represent a NULL
> object?
>
> Best regards,
> Robert
>[/color]

References are intended to "point" to existing objects :

the function

void f(T &t)
{
[...]
}

is similar to

void f(T const *t)
{
assert(t != NULL);
[...]
}

Reference can't be null and its address can't be incremented.

Bye,
Giulio
  #7  
Old August 22nd, 2005, 02:35 PM
GrahamJWalsh@gmail.com
Guest
 
Posts: n/a
Default Re: Pass by Pointer * or Pass by Reference &?

no it doesn't. On a pointer it returns null on references it throws
std::bad_cast

  #8  
Old August 22nd, 2005, 02:35 PM
msalters
Guest
 
Posts: n/a
Default Re: Pass by Pointer * or Pass by Reference &?

Frank Chang schreef:
[color=blue]
> One other difference between pointers and references is that if you use
> dynamic_cast on an incorrect reference it willl throw a bad cast
> exception. On the other hand, an incorrect dynamic cast on a pointer
> will not throw a bad cast exception.[/color]

No, it returns a NULL pointer. This can be useful:

void foo( Base* b )
{
if( Derived* d = dynamic_cast<Derived>(b) )
{
// do something with Derived
d->bar();
}
else
{
// User didn't have a Derived object, but we don't care why not
}
}
This is a quite common idiom. I usually leave out the comment and the
else block if there is no default action. The idea is that I don't
want to differentiate between a non-Derived Base object or no object
at all. I purely want to know if the caller has a Derived object.

HTH,
Michiel Salters

  #9  
Old August 24th, 2005, 04:05 PM
Dan Cernat
Guest
 
Posts: n/a
Default Re: Pass by Pointer * or Pass by Reference &?


"Robert" <zhushenli@gmail.com> wrote in message
news:1124453076.559746.224770@g47g2000cwa.googlegr oups.com...[color=blue]
> Another problem is why reference cannot be able to represent a NULL
> object?
>
> Best regards,
> Robert
>[/color]

Define what a NULL object is.

Dan


  #10  
Old August 24th, 2005, 04:05 PM
Peter Julian
Guest
 
Posts: n/a
Default Re: Pass by Pointer * or Pass by Reference &?


"Robert" <zhushenli@gmail.com> wrote in message
news:1124453076.559746.224770@g47g2000cwa.googlegr oups.com...[color=blue]
> Another problem is why reference cannot be able to represent a NULL
> object?
>
> Best regards,
> Robert
>[/color]

A reference can't refer to a null object since its permanently bound to a
valid object. This is an important distinction (unlike a pointer which can
point to another object or even to a null object). A reference is a
permanent alias to a valid object.

In fact, the lifetime of any object may even be extended to satisfy this
object-is-valid rule.

  #11  
Old August 24th, 2005, 05:05 PM
E. Robert Tisdale
Guest
 
Posts: n/a
Default Re: Pass by Pointer * or Pass by Reference &?

Robert wrote:
[color=blue]
> Another problem is
> why reference cannot be able to represent [an invalid] object?[/color]
[color=blue]
> cat main.cc[/color]
#include <iostream>

void f(const int& r) {
if (0 == &r) { // undefined behavior
std::cout << "&r = " << &r << std::endl;
}
else {
std::cout << "r = " << r << std::endl;
}
}

int main(int argc, char* argv[]) {

int* p = 0;
int& r = *p; // undefined behavior
f(r);

return 0;
}
[color=blue]
> g++ -Wall -ansi -pedantic -o main main.cc
> ./main[/color]
&r = 0

Beware!
A reference *can* represent an invalid object
but the behavior is undefined.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.