473,396 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 ?
Nov 27 '09 #1
6 4560
Banfa
9,065 Expert Mod 8TB
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.
Nov 27 '09 #2
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.
Nov 27 '09 #3
Banfa
9,065 Expert Mod 8TB
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.
Nov 27 '09 #4
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.
Nov 27 '09 #5
Banfa
9,065 Expert Mod 8TB
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.
Nov 27 '09 #6
weaknessforcats
9,208 Expert Mod 8TB
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.
Nov 27 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Tom | last post by:
The motivation for references seems clear: stop people from using nasty pointers when all they really want is a reference to an object. But C++ references are so inadequate that I'm still using...
12
by: A-PK | last post by:
Anyone know how to breakdown every modules into different assemblies and refernces. Hence, to repair problematic module, only that particular module needed to be recompiled. or any place...
33
by: JKop | last post by:
I understand variables/objects and pointer variables perfectly: int X = 5; int* pX = &X; *pX = 4; int** ppX = &pX:
2
by: S. van Beek | last post by:
Dear reader, For removing a reference in the VBA reference form I receive from Doug Steele the following code: ........... References.Remove refCurr
24
by: MLH | last post by:
Most people tell me I only need 3 references. It seems that many of my problems are related to references. I don't know what a reference is, quite frankly. I wish that, instead of about 200...
8
by: Eric Eggermann | last post by:
I'm having a problem with really large file sizes when serializing the classes that describe my little document. There are some circular references which result in the same object getting written...
9
by: igor.kulkin | last post by:
References is a relatively basic feature of C++ language. It might be a good thing to think of references as aliases to the variables. However it's good to think of references this way when you...
7
by: Earl | last post by:
Can anyone point me to some good references for coding a Winforms app to trigger replication (merge preferably) on SQL2005? I'm setting up a scenario where I want my app to allow multiple laptops...
3
by: Shilpa Sethi | last post by:
Why do we need references in C++ when we can accomplish the same task with pointers? Putting it in other words: What are the advantages of using references over pointers? How actually is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.