why do we need references ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cplusplusbytes
    New Member
    • Nov 2009
    • 5

    why do we need references ?

    Was wondering why the concept of reference was introduce in C++ and not in C. I could figure out 2 places where we can not live without reference.

    1) Copy Constructor
    2) Operator Overloading

    I am preety sure there are lot more reasons. Can we have a list o f that ?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    references are safer than pointers, it is quite hard (not impossible) to get an invalid reference, it is very easy to get an invalid pointer.

    Comment

    • cplusplusbytes
      New Member
      • Nov 2009
      • 5

      #3
      I agree, but what I am looking for is features in C++ which can not be implemented at all with out using references, like copy ctor and operator overloading.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        There aren't any that I am aware of, pretty much any feature that can be implemented with a reference can be implemented with a pointer, just in a less safe manor.

        You don't even need references for copy ctor (can use a pointer or object) or operator overloading (can take and return the object)* not that you should use any of those 2 techniques.

        *Except may be the assignment operator.

        Comment

        • cplusplusbytes
          New Member
          • Nov 2009
          • 5

          #5
          using pointer for copy ctor and operator overloading is not the right way. For copy ctor, as the purpose of it is to make a copy of object when passing it by value. If you implement it using pointer you are asking user to pass the object address and not the object itself.
          So looking for similar example why BS though of having reference in C++ and C committe never decided to implement the same in C.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            I never said that using pointers for copy ctor or operator overloading is right. But you implied that you need references for them which you don't. References are useful and good and I would recommend using them but they are not a necessity in C++, they do solve several issues in a very neat and useful way.

            Having references makes much more sense where you have objects that have methods and copy ctors and overloaded operators.

            None of that exists in C which makes a reference only a small step away from a pointer. C is a small and light language it has a minimum of constructs and functionality and there seems little point in duplicating pointer functionality with references.

            Other things in C that reduce the usefulness of references over pointers

            Since good programming practice in C tends to be that you don't pass compound objects by value.
            Since good programming practice in C tends to be that you don't assign compound objects.
            Variables can only be declared at the start of a code block.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Let's cut to the chase here.

              References are in C++ to avoid a copy when an object is passed by value.

              Why do that? Because in C if you want to use the original object you have to pass the address of that object. Then in the function you have to de-reference that pointer to update the original object. These are places for error and all of this is avoided by having a call-by-value that does not make a copy.

              Therefore, examine your function. If the function never changes the the address of the object used on the call, then in C++ you pass by reference. Only when the function changes the address of the original object do you need to pass by pointer.

              In effect, a reference eliminates ALL functions with pointer arguments replacing them with reference arguments. Now I understand to make this work, the compiler uses a pointer. The difference it the compiler ain't gonna screw up.

              Comment

              Working...