References

369 views

Published on

Basics of references in C++

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
369
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
20
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • References

    1. 1. References in c++ presentation by Frankie Onuonga twitter:FOnuonga
    2. 2. what is a reference?• a reference is an alias• when one initializes a reference they initialize it with the name of the object and anything that is done to the reference is directly applied to the object
    3. 3. definition syntax• a reference is created by writing the type of the target object followed by the reference operator(&) and then followed by the name of the reference, the equal sign followed by the name of the reference target object• references can have any legal variable name but it is preferred to put an r before so as to distinguish it • and thus int %rlocation=location
    4. 4. RULE• always initialize a reference on declaration otherwise you get a compiler error.
    5. 5. using the & on reference• the & sign is commonly known as the address of operator• it is obvious that this is used to declare the address of a variable and to declare a reference• if you ask a reference for its address it automatically retuns the address of the object
    6. 6. attempting to re-assign references• reference variables can not be re-assigned• reference variables are always aliases for their target• what appears to be re-assigned is then a new value for the target
    7. 7. RULE 2• It is not required to reference a pointer to 0 like a pointer.• some compilers will give this an ok but when you switch environments because of the direct communication with hardware this ma result to a huge error.
    8. 8. passing functionarguments by reference• recalling functions and their working is important• limitations include: • Arguments are passed by value • the return statement can return only one value
    9. 9. • passing values by reference can over come both the limitations• in c++ this can be accomplished by passing values either by reference or by use of pointers• syntax differs in passing a value by reference and by use of pointers but the use is the same
    10. 10. • rather than copy a value into the function and assigning it to the new variable withing it you can make the functions itself access this in a raw manner• when a function is passed a value either by reference or by use of pointers then what actually happens is the address is copied to the stack and not the entire object• this means the compiler then makes changes to the original object and not the local copy
    11. 11. returning multiple values• as discussed a function can only return one value• to resolve this one can pass two objects into the function by reference• by doing this it means the function works on the original object thus essentially passing two objects
    12. 12. passing by reference for efficiency• each time you pass an object into a function by reference then a copy of the object is made into memory• on returning an object from a function another copy is made• for small objects and integers there is very little overhead or performance problem
    13. 13. • with large objects such as structs or classes that are user created the copying cost can be high• the size of the user created object on the stack is the sum of all member variables, which can be in turn user created objects and passing this to the stack can be heavy in terms of memory consumption and performance
    14. 14. • with large objects the constructor and the destructor calls can be very expensive in terms of speed and use of memory
    15. 15. references as an alternative• it solves the problem of making exrtra copies and thus solves the problem of calls to the constructor and destructor• it uses constant pointers to constant objects and thus solves the problem of function changing the object• because an object will never be null it would be easier to work within functions if references were passed to it instead of pointers
    16. 16. mixing references with pointers• it is perfectly legal to declare both pointers and references in the same function parameter list along wih objects passed by value • Cat * SomeFunction (Person &theOwner, House *theHouse, int age);
    17. 17. • you must however be careful because a program with a reference to a null object might be compiled but it is invalid and the performance is unpredictable
    18. 18. THE END

    ×