Explain reference oddities, C/C++ Programming

Assignment Help:

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.

 


Related Discussions:- Explain reference oddities

Write a program to implement operator functions, Implementing Operator Func...

Implementing Operator Functions The general format of the Operator function is: return_type operator op ( argument list );   Where op is the symbol for the operator be

LOOP, DIFFERENCE BETWEEN WHILE AND DO WHILE LOOP?

DIFFERENCE BETWEEN WHILE AND DO WHILE LOOP?

Can you overload a function depend only on whether parameter, Can you overl...

Can you overload a function depend only on whether a parameter is a value or a reference? A: No. Passing by value and reference looks identical to the caller.

We want network-social app, We are seeking a talented developer/team with g...

We are seeking a talented developer/team with great experience in programming apps for both Android and iOS. Our project needs great skills and experience in making good functional

Html, world wide web commands

world wide web commands

Assignment, write a simple c++ program to that use the value to add five nu...

write a simple c++ program to that use the value to add five number .your program should prompt the user to enter these five numbers one after the other

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd