lets take method test which returns int
int test(int * p);
int test(int& p);
please can someone explain the different between above two stateme
lets take method test which returns int
int test(int * p);
int test(int& p);
please can someone explain the different between above two stateme
What do you think are the differences between the two?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Well, I'm asking you to give answering your own question a shot. Think: how would you call these two functions? If you are implementing them, how would you use the parameter if you want to access what the parameter refers to? What happens if the argument for the former is a null pointer?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
below is my testing code.in here test will take a pointer as argument.but what is the different between test1 and test 2.both take integers as argument.i cant understand different between them.thank you for help me to think it out.can you explain test1 and 2?
Code:#include<iostream> using namespace std; int test(int * p) { return (*p)+1; } int test1(int& p) { return p+1; } int test2(int p) { return p+1; } int main() { int a=1; cout<<test(&a)<<endl; cout<<test1(a)<<endl; cout<<test2(a)<<endl; }
Compile and run:Originally Posted by rukshan
What do you observe, and what does it tell you?Code:#include <iostream> using namespace std; void test(int* p) { *p = *p + 1; } void test1(int& p) { p = p + 1; } void test2(int p) { p = p + 1; } int main() { int a = 1; test(&a); cout << a << endl; a = 1; test1(a); cout << a << endl; a = 1; test2(a); cout << a << endl; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
output :test - 2
test1 - 2
test2 -2
Answer one is according to pointer Arithmetic .I can understand that.
Answer 3 is according to normal integer passing.What i think is test1 and test2 are equal am i right?
Are you sure you compiled the code that I showed you? The output that I expect is:
Code:2 2 1
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
About test: It's not necessarily pointer arithmetic if a dereference is in the expression. It means that the pointee is accessed and it could just be regular arithmetic, as is the situation here. That might be pedantic of me, but pointer arithmetic is totally different, and you could make a critical mistake.
test and test1 do the same thing, but you should notice the difference in syntax and how aliasing is different from pass by value semantics. For example, your guess at the output of the program is wrong.
Last edited by whiteflags; 04-19-2012 at 04:32 AM. Reason: I just can't get the names right...
test2(int& p); ... pass by reference. changes to p inside test2() will be reflected back to the calling function.
test1(int p); ... pass by value, a copy of the value is passed, any changes to p inside of test1() are local, the value in the calling function will not change
Kurt
Last edited by ZuK; 04-19-2012 at 08:54 AM.