[Q] when to use pointer and when to use reference?

Discussion in 'C++' started by Yuming Ma, Nov 25, 2003.

  1. Yuming Ma

    Yuming Ma Guest

    this is an interview question.

    thanks,

    ym,
     
    Yuming Ma, Nov 25, 2003
    #1
    1. Advertising

  2. Yuming Ma

    Mike Wahler Guest

    "Yuming Ma" <> wrote in message
    news:p...
    > this is an interview question.


    This is an opinion:

    Use a reference by default, only use a pointer with
    compelling reason.

    -Mike
     
    Mike Wahler, Nov 25, 2003
    #2
    1. Advertising

  3. Yuming Ma

    Ron Natalie Guest

    "Yuming Ma" <> wrote in message news:p...
    > this is an interview question.
    >

    You're not going to get that job.
     
    Ron Natalie, Nov 25, 2003
    #3
  4. Yuming Ma

    Eric Guest

    Yuming Ma <> wrote:

    > this is an interview question.


    I tend to use a reference by default.

    However, for example, when it is useful to be able to check to see if a
    parameter is NULL to help determine how a function behaves, then I will
    use a pointer.


    --
    == Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
    "Therefore the considerations of the intelligent always include both
    benefit and harm." - Sun Tzu
    == Insults, like violence, are the last refuge of the incompetent... ===
     
    Eric, Nov 25, 2003
    #4
  5. Yuming Ma

    lilburne Guest

    Yuming Ma wrote:

    > this is an interview question.
    >


    Depends on context of the question.

    Mainly if the reference needs to be 'reassigned' use a pointer.

    Then there are style issues. For example some styles would
    have you use a const reference for input parameters to a
    function and a non-const pointer for output parameters.
     
    lilburne, Nov 25, 2003
    #5
    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. sam pal
    Replies:
    3
    Views:
    593
    E. Robert Tisdale
    Jul 16, 2003
  2. shuisheng
    Replies:
    4
    Views:
    394
    peter koch
    Sep 26, 2006
  3. Replies:
    4
    Views:
    410
    Andre Kostur
    Dec 24, 2007
  4. mathieu
    Replies:
    8
    Views:
    531
    Juha Nieminen
    Aug 31, 2008
  5. A
    Replies:
    7
    Views:
    658
Loading...

Share This Page