fbpx

Here is a concise C++ debate on references versus pointers References: 1) Its necessary to initialize the Reference at the time of declaration. Like int &a = 10; int &a; //Error here but not in case of Pointer. 2) You can not create the Array of reference. 3) You can not assign NULL to the reference like int &a = NULL; //Error * Reference is an implicit pointer * Must tell it which variable it will become an alias for * References do not require dereferencing * No acquisition of memory address required * References are often implemented by the compiler writers as pointers. * References are "safer" than pointers Pointers can have advantages over references: * Pointer can be left uninitialized. * Pointer can be stored in Container classes, References cannot, as they are uninitialized. * Most programs has "NULL" values, Pointers are recommended in these cases. * Pointer can easily visualize side-effect is local or not. You initilizae non static class members that are refences in the initialization list only. Pass by reference is safer than pass by pointer since you cannot pass nulls int *p=0; foo(*p); void foo(int &x){ } in above foo() function you can change the value of x but can't make it to point to some other integer… References act an alias. Prepending variable with "&" symbol makes it as reference. When do use "const" reference arguments in function? a) Using const protects you against programming errors that inadvertently alter data. b) Using const allows function to process both const and non-const actual arguments, while a function without const in the prototype can only accept non constant arguments. c) Using a const reference allows the function to generate and use a temporary variable appropriately. When are temporary variables created by C++ compiler? Provided that function parameter is a "const reference", compiler generates temporary variable in following 2 ways. a) The actual argument is the correct type, but it isn't Lvalue double Cube(const double & num) { num = num * num * num; return num; } double temp = 2.0; double value = cube(3.0 + temp); // argument is a expression and not a Lvalue; b) The actual argument is of the wrong type, but of a type that can be converted to the correct type long temp = 3L; double value = cuberoot ( temp); // long to double conversion

(Last Updated On: June 15, 2010)

Here is a concise C++ debate on references versus pointers
References:
1) Its necessary to initialize the Reference at the time of declaration. Like int &a = 10;
int &a; //Error here but not in case of Pointer.
2) You can not create the Array of reference.
3) You can not assign NULL to the reference like int &a = NULL; //Error
* Reference is an implicit pointer
* Must tell it which variable it will become an alias for
* References do not require dereferencing
* No acquisition of memory address required
* References are often implemented by the compiler writers as pointers.
* References are “safer” than pointers
Pointers can have advantages over references:
* Pointer can be left uninitialized.
* Pointer can be stored in Container classes, References cannot, as they are uninitialized.
* Most programs has “NULL” values, Pointers are recommended in these cases.
* Pointer can easily visualize side-effect is local or not.
You initilizae non static class members that are refences in the initialization list only.
Pass by reference is safer than pass by pointer since you cannot pass nulls
int *p=0;
foo(*p);
void foo(int &x){ }
in above foo() function you can change the value of x but can’t make it to point to some other integer…
References act an alias. Prepending variable with “&” symbol makes it as reference.
When do use “const” reference arguments in function?
a) Using const protects you against programming errors that inadvertently alter data.
b) Using const allows function to process both const and non-const actual arguments, while a function without const in the prototype can only accept non constant arguments.
c) Using a const reference allows the function to generate and use a temporary variable appropriately.

When are temporary variables created by C++ compiler?
Provided that function parameter is a “const reference”, compiler generates temporary variable in following 2 ways.
a) The actual argument is the correct type, but it isn’t Lvalue
double Cube(const double & num)
{
num = num * num * num;
return num;
}
double temp = 2.0;
double value = cube(3.0 + temp); // argument is a expression and not a Lvalue;
b) The actual argument is of the wrong type, but of a type that can be converted to the correct type
long temp = 3L;
double value = cuberoot ( temp); // long to double conversion

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!
Don't miss out!
Get my TRADE LIKE A BOSS 2 PDF Books

You will received instantly the download links.

Invalid email address
Give it a try. You can unsubscribe at any time.

NOTE!

Check NEW site on stock forex and ETF analysis and automation

Scroll to Top