Although There Are Differences Between Structured Programming and Object Oriented Programming
Although There Are Differences Between Structured Programming and Object Oriented Programming
Reference (C++)
From Wikipedia, the free encyclopedia
Jump to:navigation, search
In the C++ programming language, a reference is a simple reference datatype that is less
powerful but safer than the pointer type inherited from C. The name C++ reference may
cause confusion, as in computer science a reference is a general concept datatype, with
pointers and C++ references being specific reference datatype implementations.
Contents
[hide]
• 5 External links
where <Type> is a type and <Name> is an identifier whose type is reference to <Type>.
Examples:
1. int A = 5;
2. int& rA = A;
3. extern int& rB;
4. int& foo ();
5. void bar (int& rP);
6. class MyClass { int& m_b; /* ... */ };
7. int funcX() { return 42 ; }; int (&xFunc)() = funcX;
Here, rA and rB are of type "reference to int", foo() is a function that returns a
reference to int, bar() is a function with a reference parameter, which is reference to
int, MyClass is a class with a member which is reference to int, funcX() is a function
that returns an int, xFunc() is an alias for funcX.
Types which are of kind "reference to <Type>" are sometimes called reference types.
Identifiers which are of reference type are called reference variables. To call them
variable, however, is in fact a misnomer, as we will see.
Recent Articles
Pointers and Reference looks similar but there are some difference between both of
them.
POINTER
1) Its not necessary to initialize the pointer at the time of declaration. Like
int a = 10;
Another way is :
int a = 10;
int *P;
P = &a;
A function does not have to be instantiated before it can be accessed, therefore only one
copy (or instance) is said to exist at any one time.
A class method can only be accessed after it has been instantiated into an object, and it is
possible to create multiple instances (objects) of the same class with different object
names.
A function has only a single point of entry, and that is the function name itself.
An object has multiple points of entry, one for each method name.
They have different methods of maintaining state
A function by default does not have state, by which I mean that each
time that it is called it is treated as a fresh invocation and not a continuation of any
previous invocation.
An object does have state, by which I mean that each time an object's method is called it
acts upon the object's state as it was after the previous method call.