C++ References.. Why should we use References? |
| |
 |
Reputation Power: 1

| | New Poster Posts: 5 Topics: 5 |
|
|
July 09, 2009, 10:08:29 AM
|
I recently started learning about c++ and I'm just learning about references. But I don't get them  If it's just another name for a variable, what's the point for them, I still don't see why I should use them for anything. Anyone kn ow?
« Last Edit: August 01, 2011, 06:14:00 AM by Admin »
|
|
|
|
| |
 |
Reputation Power: 17

| | Code Guru Gender:  Posts: 1403 Topics: 86  |
|
|
July 10, 2009, 01:41:11 AM
|
They are very useful and used in other languages albeit even if the programmer does not know they are manipulating them. You are right references refer to variables, or chunks of memory depending how you want to think of it.
The following code is an example of their use
int * a =malloc(sizeof(int)*10); int * c=a; int i; for (i=0;i<10;i++) { a=10 -i; printf("%d",c); }
« Last Edit: July 10, 2009, 04:15:38 AM by polas »
|
|
| |
 |
Reputation Power: 2

| | Skilled Coder Posts: 125 Topics: 0 |
|
|
June 13, 2011, 02:18:45 AM
|
According to my knowledge Reference is a more safer than pointer.
|
|
| |
 |
Reputation Power: 1

| | New Poster Posts: 5 Topics: 0  |
|
|
July 02, 2011, 05:24:22 AM
|
The standard C++ library is a collection of functions, constants, classes, objects and templates that extends the C++ language providing basic functionality to perform several tasks, like classes to interact with the operating system, data containers, manipulators to operate with them and algorithms commonly needed.
|
|
| |
 |
Reputation Power: 6

| | Professional Coder Real name: Odusee Location: Australia
Gender:  Posts: 455 Topics: 46  |
|
|
July 03, 2011, 11:02:17 PM
|
C++ references differ from pointers in several essential ways: -It is not possible to refer directly to a reference object after it is defined; any occurrence of its name refers directly to the object it references. -Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers. -References cannot be null, whereas pointers can; every reference refers to some object, although it may or may not be valid. -References cannot be uninitialized. Because it is impossible to reinitialize a reference, they must be initialized as soon as they are created. In particular, local and global variables must be initialized where they are defined, and references which are data members of class instances must be initialized in the initializer list of the class's constructor.
|
|
| |
 |
Reputation Power: 3

| | Professional Coder Posts: 227 Topics: 7 |
|
|
August 01, 2011, 02:17:20 AM
|
The use of reference is good always than pointers.
|
|
| |
 |
Reputation Power: 2

| | Regular Coder Posts: 88 Topics: 10  |
|
|
August 05, 2011, 07:16:13 PM
|
C++ references allow you to create a second name for the a variable that you can use to read or modify the original data stored in that variable. While this may not sound appealing at first, what this means is that when you declare a reference and assign it a variable, it will allow you to treat the reference exactly as though it were the original variable for the purpose of accessing and modifying the value of the original variable--even if the second name (the reference) is located within a different scope. This means, for instance, that if you make your function arguments references, and you will effectively have a way to change the original data passed into the function. This is quite different from how C++ normally works, where you have arguments to a function copied into new variables. It also allows you to dramatically reduce the amount of copying that takes place behind the scenes, both with functions and in other areas of C++, like catch clauses.
|
|
| |
 |
Reputation Power: 3

| | Professional Coder Posts: 227 Topics: 7 |
|
|
August 10, 2011, 04:06:55 AM
|
Yes I agree with it.
|
|
| |
 |
Reputation Power: 5

| | Professional Coder Posts: 244 Topics: 85  |
|
|
August 15, 2011, 12:51:06 AM
|
Most academic written work is expected to be drawn from secondary sources, that is books and periodicals. You need to acknowledge the material you use so that you readers can see where you have got your evidence from.
The Havard System is one of a number of conventions on how to set out your referencing, search for it on-line there are plenty of site which explain exactly how it works. You will proabably be told which convention to use - if not the important thing is to choose one and stick to it.
|
|
| |