13.07.2015 Views

BABAR C++ Course Running the Examples - HEPHY

BABAR C++ Course Running the Examples - HEPHY

BABAR C++ Course Running the Examples - HEPHY

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Formal ArgumentsReferencesConsider(ch2/funcarg.C)void f(int i, float x, float *a) {i = 100;x = 101.0;a[0] = 0.0;}int j = 1;int k[] = {2, 3, 4};float y[] = {3.0, 4.0, 5.0};f(j, k[0], y);• in Fortran passing k or k(1) to function is <strong>the</strong> same• in C/<strong>C++</strong> what’s <strong>the</strong> value of k[0] after callingf()?• upon calling f(), <strong>the</strong> compiler generated code toinitialize <strong>the</strong> argumentsint i = j;float x = k[0]; // note type conversionfloat *a = y; // init pointer to array• thus k[0] does not changeA way to reference <strong>the</strong> same location (<strong>C++</strong> only)Reference (ch/simplecpp.C)float x = 12.1;float& a = x;float &b = x;• a and b are called a reference• a, b, and x are all <strong>the</strong> same memory location• <strong>the</strong> position of <strong>the</strong> “&” is optional• Don’t confuse a reference and a pointerint i = 3; // data objectint &j = i; // reference to iint *p = &i; // pointer to i• i has an address of a memory location containing3• j has <strong>the</strong> same address as i• <strong>the</strong> contents of p is <strong>the</strong> address of iint *p: i:j:int3<strong>BABAR</strong> <strong>C++</strong> <strong>Course</strong> 56 Paul F. Kunz<strong>BABAR</strong> <strong>C++</strong> <strong>Course</strong> 57 Paul F. Kunz

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!