Friday, July 31, 2009

C++ parameters?

what is the difference between call-by-reference and call-by-value? another thing is call-by-reference and pass-by-reference same? as well as call-by-value and pass-by-value?





:)

C++ parameters?
HI, I can explain this to you. I've always loved C++. It can be a very powerful language. What I like about is the object support, but, inlike other languages (like C# or VB) you can still use pointers, you can in fact use generic pointers.





Let's consider the basic pointer type. Look at the following declarations ;


int s; // Sn integer


int *s_p; // pointer to an integer


void *p // generic pointer


char c; // char


char *c_p; // char pointer (possibly to an array of chars)


char *c_p[] // Pointer to an array of chars


void *AnyObject // An Object pointer





In the above variable list, the variables s and c are value variables. All the pointer types are reference variables.








So, I assume that you understand the difference between pointers and non-pointer types. Basically, a pointer is a reference. All type of pointer, regardless of what they point at, are internally represented as the same type (a memory address). A pointer can be thought of as a "reference" to the variable or object to which it points.(Object pointers are a special case,which we won;t consider here)





Suppose that a function, or method of an object, is declared like this ;





Void SolveArray(int *piArray[], int iMaxIndex)





This function takes 2 parameters, its first parameter a pointer to an array of integers and the second an integer.


The first parameter is said to be "passed by reference". The second is said to be passed by value. When the function is actually executed by the program, and for a parameter passed by value, a copy of the value in the call will be used. for parameters passed by reference, the function has an address to the actual array.





int iArray[128];


int iMax;


int *piPtr[];





LoadArray(%26amp;iArray);


iMax=128;


*piPtr = %26amp;iArray;


SolveArray(piPtr,iMax); // or SolveArray(%26amp;iArray,iMax)





IOW, if SolveArray changes something in *piArray[] it is actually changing the values in iArray. If SolveArray were to change the value of iMaxIndex, the variable iMax is not changed. This is because when the function is called, any parameter passed by value is a copy of the variable used when called. Since any paramter thart is a reference, it IS the address of the variable used in the actual call.
Reply:I hate C++


I DESPISE IT








so much!!!!!!!!
Reply:I dont know about C++ , but In vb call by reference (or ByRef) will mean that your argument is not allowed to be changed during the sub or function. It can only be read-only and hence called for reference only. call by value (or ByVal) allows you to change the arguments a function or sub recieves directly.
Reply:Well, this is definetly the wrong place to post this question, but to answer it briefly, references (similar to pointers from C) carry the reference to memory rather than the value itself. So, when you dereference a variable, and assign it a new value, it changes all variables pointing to that memory location.





This is powerful in OO programing (which is where C++ makes significant extensions over C) in that you can pass around complex objects which is just really a bunch of references and have methods reassign those values directly.


No comments:

Post a Comment