Anda di halaman 1dari 5

Pointer to constant vs.

pointer constant

Pointer to constant points to a value that does not change and is declared as:

const type * name


type is data type
name is name of the pointer
e.g: const char *p;

pointer to constant can not be used to change the value being pointed to. Therefore:
char ch = ‘A’;
const char *p = &ch;
*p = ‘B’;

is not allowed. The program will throw an error.

Pointer Constant (or constant pointer) is a pointer which you don’t want to be pointed to
a different value. That is, the location stored in the pointer can not change. We can not
change where the pointer points. It is declared as:

type * const name


type is data type
name is name of the pointer
eg: char * const p

Since the location to which a const pointer points to can not be changed, the following
code:

char ch1 = ‘A’;


char ch2 = ‘B’;
char * const p = &ch1;
p = &ch2;
will throw an error since address stored in p can not be changed.

C++ - Pointer vs. reference - Feb 23, 2010 at 11:00 PM by Rajmeet Ghai

Pointer vs. reference.

When a reference is created, it can’t reference another object. This can be done with
pointers. References cannot be null whereas pointers can be. References cannot be
uninitialized and it is not possible to refer directly to a reference object after it is defined.

Example:
Syntax of reference:
<Type> & <Name>
int& rA = A; rA is a reference to int.

C++ - Pointer vs. reference - March 08, 2009 at 18:50 PM by Nishant Kumar
Pointer vs. reference.

A reference must be initialized at the time of declaration whereas not needed in case of
pointer. A pointer can point to a null object.

A reference once initialize to refer to the object can't be reassigned to refer to other
whereas reassignment is possible with pointer.

C++ - Explain the use of this pointer - August 05, 2008 at 22:10 PM by Amit Satpute

Explain the use of this pointer.

The this keyword is used to represent an object that invokes the member function. It
points to the object for which this function was called. It is automatically passed to a
member function when it is called.
e.g. when you call A.func(), this will be set to the address of A.

C++ - Explain the use of this pointer - Jan 07, 2009 at 18:10 PM by Anuja Changede

When a member function is called, it is automatically passed an implicit argument that is


a pointer to the invoking object (ie the object on which the function is invoked). This
pointer is known as this pointer. It is internally created at the time of function call.

The this pointer is very important when operators are overloaded.

Example: Consider a class with int and float data members and overloaded Pre-increment
operator ++:

class MyClass
{
int i;
float f;
public:
MyClass ()
{
i = 0;
f = 0.0;
}
MyClass (int x, float y)
{
i = x; f = y;
}
MyClass operator ++()
{
i = i + 1;
f = f + 1.0;
return *this; //this pointer which points to the caller object
}
MyClass show()
{
cout<<”The elements are:\n” cout<i<<”\n<f; //accessing
data members using this
}
};

int main()
{
MyClass a;
++a; //The overloaded operator function ++()will return a’s this
pointer
a.show();
retun 0;
}
The o/p would be:
The elements are:
1
1.0

C++ - When do you use this pointer? - Feb 23, 2009 at 22:45 PM

When do you use this pointer?

Answer - 'this pointer' is used as a pointer to the class object instance by the member
function. The address of the class instance is passed as an implicit parameter to the
member functions.

C++ - Explain passing objects by reference, passing objects by value and passing objects by
pointer - April 04, 2009 at 18:00 PM by Vidya Sagar

Explain passing objects by reference, passing objects by value and passing objects
by pointer.

Pass by value:

The callee function receives a set of values that are to be received by the parameters. All
these copies of values have local scope, i.e., they can be accessed only by the callee
function. The simplicity and guarantee of unchanging of values passed are the advantages
of pass by value.

Pass by reference:

The callee function receives a set of references which are aliases to variables. If a change
is made to the reference variable, the original value (passed by the caller function) will
also be changed. All the references are handled by the pointers. Multiple values
modification can be done by passing multiple variables.
Pass by pointer:

The callee function receives a pointer to the variable. The value of the pointer in the
caller function can then be modified. The advantages of this process are that the changes
are passed back to the caller function and multiple variables can be changed.

C++ - What is reference variable in C++? - April 16, 2009 at 16:40 PM

What is reference variable in C++?

A reference variable is just like pointer with few differences. It is declared using &
operator. A reference variable must always be initialized. The reference variable once
defined to refer to a variable can’t be changed to point to other variable. You can't create
an array of references the way it is possible with pointer.

Explain how to call C functions from C++

The extern keyword is used to call C functions from C++

extern "C" void showme()

C++ - What is pointer? Explain with examples - Jan 07, 2009 at 18:10 PM by Anuja Changede

What is pointer? Explain with examples

A pointer is a variable that holds a memory address. This address is the location of
another object (typically, a variable) in memory. That is, if one variable contains the
address of another variable, the first variable is said to point to the second.

A pointer declaration consists of a base type, an *, and the variable name. The general
form of declaring a pointer variable is:

type *name;
type is the base type of the pointer and may be any valid type.
name is the name of pointer variable.
The base type of the pointer defines what type of variables the pointer can point to.

Two special pointer operators are: * and &.


The & is unary operator that returns the memory address of its operand. It is “the address
of” operand. The * is complement of &. It is also a unary operator and returns the value
located at the address that follows.

int i, *p;
i = 5;
p = &i; //places the memory address of i into p

The expression *p will return the value of variable pointed to by p.

Anda mungkin juga menyukai