|
|
some of them have already been mentioned in this thread.
One more is
* reference has to be initialized when defined, but pointer need not be
e.g.
const int* p;
int& l; //error here
|
|
|
|
|
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=1;
cout<<a; In this example a and b 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; f is not doing what you intended because you are passing the value of a , not the variable itself (in terms of implementation, you're passing an int constructed from a .) What f needs is the "name" of the object:
void f(int& i)
{
i=1;
}
int a=0;
f(a);
cout<<a; Get 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.
|
|
|
|
|
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);
...
f(something);
f(&something); If I change the definition of foo to allegedly make it more robust, the whole program has to be changed as
void 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 to foo .) 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
int * p;
int& p=...
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.)
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Well what I meant was
<br />
void FOO(int* p){*p+=1;}<br />
<br />
int* i = NULL;<br />
FOO(i)<br />
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.
|
|
|
|
|
Can anybody tell me structure of make file how it is used and link with different environment
thanx
|
|
|
|
|
|
Hi everybody;P
I want to transfer a byte to parallel port, but i can't. Somebody help me do it.
Thanks,
hung
|
|
|
|
|
|
I can't link the URL that you sent
hung
|
|
|
|
|
Neither can I.
Try this
http://www.codeproject.com/csharp/csppleds.asp
|
|
|
|
|
hi,
i want to know any simple image capture API or method so that i can get capture image from a USB camera?
very simply! just capture image, return bitmap array or file handle... that's all!
need to use API in directdraw?
any help?
thanks,
jim
|
|
|
|
|
Tbanks a lot! how to display a text at the mouse?
|
|
|
|
|
GetMousePos, from memory, tells you the position. The in OnPaint, draw whatever text you want.
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
Your memory is going!
Its GetCursorPos()
Roger Allen
Sonork 100.10016
Death come early, death come late,
It takes us all, there is no reason.
For every purpose under heaven,
To each a turn, to each a season.
A time to weep and a time to sigh,
A time to laugh and a time to cry,
A time to be born and a time to die.
Dust to dust and ashes to ashes,
And so I end my song.
|
|
|
|
|
Can someone PLEASE tell me how to modify C++ code for prims Algorithm to tell if it's connected or not ? I know this is probably really simple, but I am having major codewriters block trying to figure it out!! HELP ME!!!
|
|
|
|
|
It being the graph.
|
|
|
|
|
Prim's algorithm computes a minimum spanning tree, starting from whatever vertex and adding one edge at a time. When the algorithm is done, you can verify whether the number of vertices in the spanning tree is equal to the number of vertices in the graph: if it is not, the graph is not connected.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
I can't find any information on this subject.
I want to call a standalone exe from a dialog application.
For example I want to be able to call a media program from mine
like winamp or some standalone video player.
As a side note if I call an exe from a full screen dialog application how do
I give the exe the ability to go full screen or how do I temporally tell my
full screen dialog application to go away for awhile?
Thanks,
Steve
|
|
|
|
|
Go to MSDN and look up ShellExecute or CreateProcess. CreateProcess is much more complicated, but also more powerful - and you can wait for processes to finish with it. But ShellExecute is simpler, and may be all you need here.
If your nose runs and your feet smell, then you're built upside down.
|
|
|
|
|
That should do, thanks,
Steve
|
|
|
|
|
|
Hi,
I have a 3D object in OpenGL which I display in multiple view (top, bottom, left and right). Because my object is much longer than thick, I would like to expand the X-axis coordinates for the left and right view.
I only use one display list for all view and views have their own display context. So how can I change the appearance of the X-axis for specific view without rebuilding the display list ??
Thank you guys!
Jean Bédard
Optifor, Inc
Québec, Canada
|
|
|
|
|
I have problem with formview printing. 've downed the file and
try to print out the formview in my mdi application. However,
only i can print out is the control( gray colored ). Actually,
there is image on this control and this program can't print out
image but only control itself. does anyone have the same problem
and solve this problem before?
thanks in advance.
shin
// TODO: Add your command handler code here
CRect rect;
this->GetClientRect( rect );
CDC memDC;
CClientDC dc( this );
memDC.CreateCompatibleDC( &dc );
CBitmap bitmap;
bitmap.CreateCompatibleBitmap( &dc, rect.Width(), rect.Height() );
{
LocalGDI local( &memDC, &bitmap );
this->Print( &memDC, PRF_ERASEBKGND|PRF_CLIENT|PRF_CHILDREN );
}
m_dib.Attach( GDIUtil::DDBToDIB( bitmap ) );
CFormView::OnFilePrintPreview( );
|
|
|
|