c++ - What happens when use a function with an argument by reference with a pointer? -
references functions rather confusing. can put 1 in argument in function definition, put non-reference non-pointer value in argument when use later. , thought this:
int foo (int& bar) { //code } //more code... int* = &b; c = foo (a); what happens? foo use a, b, or what? how force either use?
why don't test yourself? whenever you're in doubt, try compiling it!
it won't work , makes sense - foo() asking parameter of type int& - reference int, trying pass pointer it. error
cannot convert parameter 1 'int *' 'int &'
regarding question
how force either use?
your function able modify int value passed it, because accepts reference parameter.
if don't want function able modify argument, change signature to:
int foo (int bar)
or change parameter reference const int:
int foo (const int& bar)
Comments
Post a Comment