• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

How To Use Reference To An Object In C++

flesing

Infrastructure Builder
F Rep
0
0
0
Rep
0
F Vouches
0
0
0
Vouches
0
Posts
156
Likes
23
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
  1. Time sunset(

    19

    ,0

    ,0

    )

    ;

    // object of type Time
  2. Time &

    dinnerTime =

    sunset;

    // reference to a Time object

Defining a reference to an object is equal to defining an alias to this object. We can use the reference in the left side of an assignment, that means we can assign a new value to this reference and change the original object that is referenced.
We can write a public member function in a class that returns a reference to private data member, let us have the following class:
  1. class

    Time {
  2. public

    :
  3. // Constructor function to initialize private data.
  4. // Calls member function setTime to set variables.
  5. // Default values are 0 (see class definition).
  6. Time(

    int

    hr,int

    min, int

    sec )
  7. {
  8. setTime(

    hr, min, sec )

    ;
  9. }
  10. // Set the values of hour, minute, and second.
  11. void

    setTime(

    int

    h,int

    m, int

    s )
  12. {
  13. hour =

    (

    h >=

    0

    &&

    h <

    24

    )

    ?

    h :

    0

    ;
  14. minute =

    (

    m >=

    0

    &&

    m <

    60

    )

    ?

    m :

    0

    ;
  15. second =

    (

    s >=

    0

    &&

    s <

    60

    )

    ?

    s :

    0

    ;
  16. }
  17. // Get the hour value
  18. int

    getHour(

    )
  19. {
  20. return

    hour;
  21. }
  22. // POOR PROGRAMMING PRACTICE:
  23. // Returning a reference toa private data member.
  24. int

    &

    badSetHour(

    int

    hh)

    // DANGEROUS reference return
  25. {
  26. hour =

    (

    hh >=

    0

    &&

    hh <

    24

    )

    ?

    hh :

    0

    ;
  27. return

    hour;

    // DANGEROUS reference return
  28. }
  29. private

    :
  30. int

    hour;
  31. int

    minute;
  32. int

    second;
  33. }

    ;

You can notice that badSetHour() member function returns a reference to 'hour' data member, so any change has been made on this reference will change the value of 'hour' data member like in the following example:

  1. int

    main(

    )
  2. {
  3. Time t;
  4. int

    &

    hourRef =

    t.badSetHour

    (

    20

    )

    ;

  5. cout

    <<

    "Hour before modification: "

    <<

    hourRef;
  6. hourRef=

    30

    ;

    // modification with invalid value
  7. cout

    <<

    "\n

    Hourafter modification: "

    <<

    t.getHour

    (

    )

    ;

  8. // Dangerous: Function call that returns
  9. // a reference can be used as anlvalue!
  10. t.badSetHour

    (

    12

    )

    =

    74

    ;
  11. cout

    <<

    "\n

    \n

    *********************************\n

    "
  12. <<

    "POOR PROGRAMMING PRACTICE!!!!!!!!\n

    "
  13. <<

    "badSetHour as an lvalue, Hour: "
  14. <<

    t.getHour

    (

    )
  15. <<

    "\n

    *********************************"

    <<

    endl;

  16. return

    0

    ;
  17. }

The output:

Hour before modification: 20
Hour after modification: 30
*********************************
POOR PROGRAMMING PRACTICE!!!!!!!!
badSetHour as an lvalue, Hour: 74
*********************************

Note: You can find the full source code of this example in code.zip file.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,512

356,407

356,429

Top
Raidforums