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

Define difference among delete and delete[]?, when you allocate memory with...

when you allocate memory with new[], you ought to free the memory via delete[]. While you allocate memory along 'new', then use 'delete' with no the brackets. You employ new[] to a

Flow chart, how made a flow chart of prime number by using for loop

how made a flow chart of prime number by using for loop

I need csgo wallhack and human aimbot, I need CSGO Wallhack and Human Aimbo...

I need CSGO Wallhack and Human Aimbot Project Description: Need a CSGO hack which can ESP Wallhack and Aimbot realistically. Must be VAC Proof, and ESL proof, as wel

What are the benefits of using friend classes, What are the benefits of usi...

What are the benefits of using friend classes? - Friend classes are useful when a class wants to hide features from users which are required only by another, tightly coupled cl

Program of file data, Program of file data: FileData::FileData( ffblk&...

Program of file data: FileData::FileData( ffblk& blk ) :     fileName( blk.ff_name ),     fileDate( (blk.ff_fdate >> 5) & 0x000F,               blk.ff_fdate & 0x0001F

C++ program, Receive 3 numbers and display them in ascending order from sma...

Receive 3 numbers and display them in ascending order from smallest to largest ed#

Common c control flow instructions, In this Lab you will code 8086 assemble...

In this Lab you will code 8086 assembler equivalents (Virgo) for common C control flow instructions. This will give you an appreciation for how control flow is implemented in assem

List, Is there any difference between List x; and List x ()? Can you please...

Is there any difference between List x; and List x ()? Can you please provide me assignment help.

Source code on home stay, I am doing project on home stay which includes fe...

I am doing project on home stay which includes features like add homestay,search,book,delete homestay,

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