what is difference betwn pointer and referece
-
i think reference is address of the object.. unlike pointer, u cannot delete reference pointer is links to memory but reference sit on the top of the object.. reference never be the pointer , reference just another name of the object.
-
i think reference is address of the object.. unlike pointer, u cannot delete reference pointer is links to memory but reference sit on the top of the object.. reference never be the pointer , reference just another name of the object.
-
what's that conditions
-
basic things of reference and pointer but can anybody explain me how they operate differently thax
References may be implemented as pointers (though not constant ones as someone has said), but this is unimportant from the user's point of view. I guess you already know what a pointer is, so let's focus on references. You can think of a reference to an object as an alias to that object, i.e. an alternate name for the object. So:
int a=0;
int& b=a; // b is a new name for a
b=1;
cout<<a; // outputs 1In this example
a
andb
refer to the very same object. There is a common use of references as convenient arguments to function. Consider:void f(int i)
{
i=1;
}int a=0;
f(a);
cout<<a; // outputs 0!f
is not doing what you intended because you are passing the value ofa
, not the variable itself (in terms of implementation, you're passing anint
constructed froma
.) Whatf
needs is the "name" of the object:void f(int& i)
{
i=1;
}int a=0;
f(a);
cout<<a; // outputs 1Get the idea? Of course you could have gotten the same effect using pointers, in this case at least it is a matter of convenience and style which solution you use. References are somewhat hard to grasp, keep on reading good C++ stuff and the concept will progressively build in your mind. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
what's that conditions
-
References may be implemented as pointers (though not constant ones as someone has said), but this is unimportant from the user's point of view. I guess you already know what a pointer is, so let's focus on references. You can think of a reference to an object as an alias to that object, i.e. an alternate name for the object. So:
int a=0;
int& b=a; // b is a new name for a
b=1;
cout<<a; // outputs 1In this example
a
andb
refer to the very same object. There is a common use of references as convenient arguments to function. Consider:void f(int i)
{
i=1;
}int a=0;
f(a);
cout<<a; // outputs 0!f
is not doing what you intended because you are passing the value ofa
, not the variable itself (in terms of implementation, you're passing anint
constructed froma
.) Whatf
needs is the "name" of the object:void f(int& i)
{
i=1;
}int a=0;
f(a);
cout<<a; // outputs 1Get the idea? Of course you could have gotten the same effect using pointers, in this case at least it is a matter of convenience and style which solution you use. References are somewhat hard to grasp, keep on reading good C++ stuff and the concept will progressively build in your mind. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
I would say it is not a matter of style and convenience. In most circumstances only pass pointers into functions if you need the possibility that the object is not valid, i.e. null pointer. It is a matter of the compiler catching errors or having them show up at runtime.
-
I would say it is not a matter of style and convenience. In most circumstances only pass pointers into functions if you need the possibility that the object is not valid, i.e. null pointer. It is a matter of the compiler catching errors or having them show up at runtime.
It is a matter of the compiler catching errors or having them show up at runtime. I don't think substituting references for pointers in function declarations catches any error at runtime. For instance, consider a program like this
void foo(int *i);
...
// invocations of f have one of these two forms
f(something);
f(&something);If I change the definition of
foo
to allegedly make it more robust, the whole program has to be changed asvoid foo(int& i);
...
f(*something);
f(something);Any program compiling with the first definition of
foo
will compile just the same with the second definition (after modifying the invocations tofoo
.) So, you won't catch more errors at compile time. References can help prevent problems at compile time, but not when used as function arguments --rather, when replacing pointer variables declarations with reference variables declarations, as in// version 1
int * p;// version 2
int& p=... // it is mandatory that p be initialized, which may help preserve
// the sanity of the programAnyway replacing pointers with references is not always feasible, if the concept of null object is valid, or if the pointer must be rebound (references cannot.) Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
It is a matter of the compiler catching errors or having them show up at runtime. I don't think substituting references for pointers in function declarations catches any error at runtime. For instance, consider a program like this
void foo(int *i);
...
// invocations of f have one of these two forms
f(something);
f(&something);If I change the definition of
foo
to allegedly make it more robust, the whole program has to be changed asvoid foo(int& i);
...
f(*something);
f(something);Any program compiling with the first definition of
foo
will compile just the same with the second definition (after modifying the invocations tofoo
.) So, you won't catch more errors at compile time. References can help prevent problems at compile time, but not when used as function arguments --rather, when replacing pointer variables declarations with reference variables declarations, as in// version 1
int * p;// version 2
int& p=... // it is mandatory that p be initialized, which may help preserve
// the sanity of the programAnyway replacing pointers with references is not always feasible, if the concept of null object is valid, or if the pointer must be rebound (references cannot.) Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
Well what I meant was
void FOO(int* p){*p+=1;} int* i = NULL; FOO(i)
If you use references then this sort of problem just can not occur. So it is not that an error will be caught at compile time, there can be no error if you use references. Anyway replacing pointers with references is not always feasible, if the concept of null object is valid, or if the pointer must be rebound (references cannot.) I agree and that is what I said.