C++ pointer vs reference

This is a discussion on C++ pointer vs reference within the C++ Programming forums, part of the General Programming Boards category; Hi, I am new to C++, and I think it will take several years for me to be profient with ...

  1. #1
    C-Dumbie
    Guest

    C++ pointer vs reference

    Hi,
    I am new to C++, and I think it will take several years for me to
    be profient with C++. My first struggle with C++ is pointer and
    reference. Althoug I think there is no different between using pointer or reference in C++, but I think each has its own place and purpose which the author of creating this C++ language has
    something intended with each type.
    Do you guys, experienced programmer, help to explain me what
    the difference between them. When I can use Pointer and when
    I can use reference. Does it difference because pointer is more efficience in execution speed than reference.
    For example below that I have learn in the book but not quite clearly understanding:
    Code:
    class A {
                  public:
                      A(); 
                      int getnum();
                 private:
                      int number;
               }; 
    
    class B  {  public:    B();
                                   void setData();
                    private: 
                       float data1;
                 };
    
    class C  {
                    public:
                      C();
                      setAlldata();
    
                   private:
                      A  *PtrToA;          //what the difference between these
                      B  &ReferenceToB;   //two? Why not use both pointers 
                };
    Your help is grateful.
    C-Dumbie

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Both pointers and references return an address of memory. Pointers can be repointed, references cannot. Hence, references are considered safer since you cannot return a NULL reference. There is no speed penalty for using one over the other. References have the added language facility of being manipulated with the "." operator and with the "*" dereference.
    Here are two identical snippets:

    Code:
    Obj * Foo( int * i ) {
      if( *i > 0) {
        *i = 1; 
       } else {
        *i = 0;
       }
     return this;
    }
    
    
    
    Obj& Foo( int& i ) {
      if( i > 0) {
         i = 1; 
       } else {
         i = 0;
       }
     return *this;
    }

    Thus references have the added benefit of easier syntax.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    *i = 1
    Shouldn't we use i=1?
    Because in *i=1, *i means the address which i is pointing to not the content of the address i is pointing to, I'm confused so please tell me.

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by ammar
    Because in *i=1, *i means the address which i is pointing to not the content of the address i is pointing to, I'm confused so please tell me.
    The opposite.
    truth = inverse(your_statement);
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Thanks for the clarification, I'm always confised with ponters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Using Vectors. MinGW warning
    By Viewer in forum C++ Programming
    Replies: 9
    Last Post: 03-26-2009, 03:15 PM
  3. Passing a pointer as a reference
    By hYph3n in forum C++ Programming
    Replies: 5
    Last Post: 10-04-2006, 01:45 PM
  4. Strange/false errors
    By Ganoosh in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2005, 04:54 PM
  5. Textbox
    By maxorator in forum Windows Programming
    Replies: 20
    Last Post: 09-25-2005, 10:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21