DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    fushin Guest

    Pointer vs. Reference


    When should I use a pointer instead a reference, or vis versa?

  2. #2
    Danny Kalev Guest

    Re: Pointer vs. Reference

    In C++, you should use a pointer only when you have no other choice,
    e.g., when allocating objects dynamically, or when a null value is
    valid. Otherwise, use references. When passing an immutable argument by
    reference, state that explicitly by using const:

    void f(const string & s);


    Danny

    fushin wrote:
    >
    > When should I use a pointer instead a reference, or vis versa?


  3. #3
    Steve Wade Guest

    Re: Pointer vs. Reference

    References are much better where you can use them as it makes the code
    easier to read and you don't have to use brackets and dereferencing to cause
    overloaded operator such as = and == to be called. Stroustrup cited the
    reference operator as 'syntactic sugar' - it makes the syntax sweeter.

    Steve

    "fushin" <fushin@aol.com> wrote in message news:3a5ded7c$1@news.devx.com...
    >
    > When should I use a pointer instead a reference, or vis versa?




  4. #4
    Mike Guest

    Re: Pointer vs. Reference


    Please correct me if I'm wrong, but I thought pointers and reference were
    synonymous. Should the question be "When should I use a pointer instead
    of a value, or vise versa?"

    In terms of memory usage, it is more efficient to pass pointers to a variable
    instead of the variable itself.

    For example:
    // main.cpp : Pass by reference or pass by value
    #include <iostream>
    using namespace std;

    int funcThatUsesPrt(double* str)
    {
    cout << *str;
    return 0;
    }

    int funcThatUsesValue(double str)
    {
    cout << str;
    return 0;
    }

    void main()
    {
    double MyDouble;
    double* pToMyDouble;

    MyDouble = 999.999;

    pToMyDouble = &MyDouble;

    funcThatUsesPrt(pToMyDouble);
    funcThatUsesValue(MyDouble);
    }

    If you use funcThatUsesPrt, you are passing 4 bytes. If you are useing funcThatUsesValue,
    you are passing 8 bytes.

    This may seem insignificant, but lets say you define a class that contains
    various member variables as follows:

    class MyClass
    {
    private:
    double x;
    double y;
    double z;
    double angle;
    public:
    // member functions go here
    };

    An instance of MyClass will be 16 bytes and a pointer to an instance of MyClass
    will be 4 bytes. Notice that a pointer to a complex class is the same as
    a pointer to a double. If fact, a pointer of any type is always 4 bytes
    (in a windows environment).

    I hope this helps.

    Mike

  5. #5
    Steve Wade Guest

    Re: Pointer vs. Reference

    In compiler terms, that's how they would be implemented but there are key
    differences between the two:

    1) A reference is an alias for an existing object and can't be made to
    reference another
    2) A reference must be intialiased to an object and, therefore, can't be
    NULL (unless the object resides at NULL)
    3) A reference causes overloaded operators to be used without dereferencing
    the 'pointer'.

    References make the code easier to read and write and reduce mistakes in my
    experience. Use references where you can.

    "Mike" <mike.higa@kyocera.com> wrote in message
    news:3a5e2119$1@news.devx.com...
    >
    > Please correct me if I'm wrong, but I thought pointers and reference were
    > synonymous. Should the question be "When should I use a pointer instead
    > of a value, or vise versa?"
    >
    > In terms of memory usage, it is more efficient to pass pointers to a

    variable
    > instead of the variable itself.
    >
    > For example:
    > // main.cpp : Pass by reference or pass by value
    > #include <iostream>
    > using namespace std;
    >
    > int funcThatUsesPrt(double* str)
    > {
    > cout << *str;
    > return 0;
    > }
    >
    > int funcThatUsesValue(double str)
    > {
    > cout << str;
    > return 0;
    > }
    >
    > void main()
    > {
    > double MyDouble;
    > double* pToMyDouble;
    >
    > MyDouble = 999.999;
    >
    > pToMyDouble = &MyDouble;
    >
    > funcThatUsesPrt(pToMyDouble);
    > funcThatUsesValue(MyDouble);
    > }
    >
    > If you use funcThatUsesPrt, you are passing 4 bytes. If you are useing

    funcThatUsesValue,
    > you are passing 8 bytes.
    >
    > This may seem insignificant, but lets say you define a class that contains
    > various member variables as follows:
    >
    > class MyClass
    > {
    > private:
    > double x;
    > double y;
    > double z;
    > double angle;
    > public:
    > // member functions go here
    > };
    >
    > An instance of MyClass will be 16 bytes and a pointer to an instance of

    MyClass
    > will be 4 bytes. Notice that a pointer to a complex class is the same as
    > a pointer to a double. If fact, a pointer of any type is always 4 bytes
    > (in a windows environment).
    >
    > I hope this helps.
    >
    > Mike




  6. #6
    Danny Kalev Guest

    Re: Pointer vs. Reference

    There is a confusion that stems from other languages in which pas by
    reference is implicit (e.g., Pascal). In C++, references and pointers
    have distinct syntactic forms:

    int n;
    int & ref= n; // ref is a reference
    int *p = &n;; // p is a pointer

    As Steve noted, there are certain restrictions (and guarantees) that
    apply to references which do not apply to pointers, thus C++ has three
    mechanisms of passing and returning variables/objects: by value, by
    address and by reference.

    Danny

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links