reference odditiesa reference variable can demote


Reference Oddities

A reference variable can demote to any integer variable, be it in an array or a member variable from structure or class. Reference variables can demote to constants as well. For instance, take a look at the program given below.

                void func(int &intref)

                 {

                                ++intref;

                 }

                void main()

                 {

                  long lint = 20;

                  int sint = 10;

                                cout << "Before incrementing " << endl;

                                cout << "Small int = " << sint << endl;

                                cout << "Long  int = " << lint << endl;

                                func(sint);                                                           // Integer Variable

                                func(lint);                                                            // Long    Variable

                                cout << "After incrementing " << endl;

                                cout << "Small int = " << sint << endl;

                                cout << "Long  int = " << lint << endl;

 

                                func(25);                                                                              // Integer Constant

                                func(sint + 10);                                                  // Expression

                }

                Output :

                                                Before incrementing

                                                Small Int = 10

                                                Long  Int = 20

                                After incrementing

                                                Small Int = 11

                                Long  Int = 20

The program calls func() , which receives a reference to an integer, four times. First with an integer variable  , which gets duly increased, as can be seen from the output. It again calls the func() with long variable.  The compiler does not give a type mismatch error but instead , it makes a temporary variable of the correct type and assigns the value of the variable lint to this variable. The func() increments the temporary variable and thus as can be seen from the output , lint still has the same value. Same, the last two calls do not result in errors but temporary variables are created for them just like in the earlier case.

Though, since a mismatch between the data types of the actual argument and the formal argument is shielded by these temporary reference variables, programmers prefer to use pointers, if a function has to change the value of an argument. Besides having the pointer notation also makes it obvious that the function is intended to make changes to the arguments. Though, references are still used with structures , classes and objects extensively.

 

Request for Solution File

Ask an Expert for Answer!!
C/C++ Programming: reference odditiesa reference variable can demote
Reference No:- TGS0309418

Expected delivery within 24 Hours