This page shows some simple examples for pass a variable
to a function by reference.
It is implemented as part of a c++
series of exercises
, that I did.
-
The code was taken from an
ECC project.
Example one is a function that counts the number of
1 bits in an unsigned char.
- void c_e_ccc_correct::bit_count(const unsigned char& d) {
- unsigned char i, j;
- bit_cnt=0;
- for(i = 0, j=1; i <= 7; i++) {
- if( d & j ) bit_cnt++;
- j *= 2;
- }
- }
-
This example was taken also from the ECC code. This
time two classes are passed by reference.
- c_e_ccc_correct::c_e_ccc_correct(const c_ecc& prev, const c_ecc& curr) {
- total_bit_cnt=0;
- for(i=0; i <= 2; i++) {
- ecc_c[i]=prev.ecc[i] ^ curr.ecc[i];
- #ifdef DGB_1 //{
- cout << "xor " << hex << (unsigned)ecc_c[i] << " " << (unsigned)prev.ecc[i] << " " << (unsigned)curr.ecc[i] << endl;
- #endif //}
- bit_count(ecc_c[i]);
- total_bit_cnt += bit_cnt;
- }//for
|