Pass by Pointer * or Pass by Reference &?

Discussion in 'C++' started by Robert, Aug 19, 2005.

  1. Robert

    Robert Guest

    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
    Robert, Aug 19, 2005
    #1
    1. Advertising

  2. "Robert" <> schrieb im Newsbeitrag
    news:...
    > 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
    >

    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
    Christian Meier, Aug 19, 2005
    #2
    1. Advertising

  3. Robert

    John Ratliff Guest

    Robert wrote:
    > 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
    >


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

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

    --John Ratliff
    John Ratliff, Aug 19, 2005
    #3
  4. Robert

    Robert Guest

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

    Best regards,
    Robert
    Robert, Aug 19, 2005
    #4
  5. Robert

    Frank Chang Guest

    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)
    {

    }
    }
    Frank Chang, Aug 20, 2005
    #5
  6. Robert

    Peter Julian Guest

    "Robert" <> wrote in message
    news:...
    > Another problem is why reference cannot be able to represent a NULL
    > object?
    >
    > Best regards,
    > Robert
    >


    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.
    Peter Julian, Aug 21, 2005
    #6
  7. Robert

    Dan Cernat Guest

    "Robert" <> wrote in message
    news:...
    > Another problem is why reference cannot be able to represent a NULL
    > object?
    >
    > Best regards,
    > Robert
    >


    Define what a NULL object is.

    Dan
    Dan Cernat, Aug 22, 2005
    #7
  8. Robert ha scritto:
    > Another problem is why reference cannot be able to represent a NULL
    > object?
    >
    > Best regards,
    > Robert
    >


    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
    Giulio Guarnone, Aug 22, 2005
    #8
  9. Robert

    msalters Guest

    Frank Chang schreef:

    > 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.


    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
    msalters, Aug 22, 2005
    #9
  10. Robert

    Guest

    no it doesn't. On a pointer it returns null on references it throws
    std::bad_cast
    , Aug 22, 2005
    #10
  11. Robert wrote:

    > Another problem is
    > why reference cannot be able to represent [an invalid] object?


    > cat main.cc

    #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;
    }

    > g++ -Wall -ansi -pedantic -o main main.cc
    > ./main

    &r = 0

    Beware!
    A reference *can* represent an invalid object
    but the behavior is undefined.
    E. Robert Tisdale, Aug 24, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry
    Replies:
    20
    Views:
    7,880
    Roedy Green
    Sep 9, 2005
  2. blufox
    Replies:
    2
    Views:
    531
  3. Mr A
    Replies:
    111
    Views:
    2,050
  4. LuB
    Replies:
    6
    Views:
    438
    Mike Wahler
    Sep 23, 2005
  5. A
    Replies:
    7
    Views:
    620
Loading...

Share This Page