Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

References #13

Closed
jimin-kiim opened this issue Dec 15, 2022 · 2 comments
Closed

References #13

jimin-kiim opened this issue Dec 15, 2022 · 2 comments

Comments

@jimin-kiim
Copy link
Owner

jimin-kiim commented Dec 15, 2022

@jimin-kiim
Copy link
Owner Author

jimin-kiim commented Dec 15, 2022

Reference Operator '&'

& :

  1. used in declaration -> reference type
  2. used not in declaration -> operator. ‘address of’ (unary operator)
  3. bitwise AND.
  • reference : alias for an object
  • frequently used for pass-by-reference
  • can be used to avoid the run-time impact of passing large arguments by value
  • parameters, return values and variables can all be defined as 'references'
double square (double &x) { return x *= x; }

int bar (void) {
  double foo = 10.0;
  square (foo);
  cout << foo; // prints 100.0
}
  • A function can also return a reference to an object
  • References are implemented similarly to const pointers.
  • In general, use of references is safer, less ambiguous, and more restricted than pointers
    • References must refer to an actual object, but pointers can refer to lots of other things that aren’t objects,
    • e.g., Pointers can refer to the special value 0 in C++ (often referred to as NULL)
void printDouble(const double& rd) 
{ cout << rd; } // no need to test rd; it must refer to a double

void printDouble(const double *pd)
{ if (pd) cout << *pd ; } // check for null pointer

@jimin-kiim
Copy link
Owner Author

jimin-kiim commented Dec 15, 2022

Argument Passing Modes

  • void X::f(T arg)           // pass by value
    • guarantees that the value of arg would not be changed
    • can be modified inside the function
  • void X::f(const T arg)     // pass by value
    • guarantees that the value of arg would not be changed
    • cannot be modified even inside the function
  • void X::f(T& arg)          // pass by reference
    • when I want to change the value of arg inside the function
    • or when I want to transfer a big object
  • void X::f(const T& arg)    // pass by reference
    • when I want to transfer a big object
    • but when I don't want the value to be changed inside the function
  • void X::f(T* argp)         // pass by pointer
  • void X::f(const T* argp)   // pass by pointer
  • void X::f(T* const argp)   // pointer is constant
    • the address value of the pointer is const
  • void X::f(const T* const argp)
    • the object transferred is const

Selecting correct mode for arguments

  • passing primitive type arguments or small objects
    -> pass by value
    • safer
  • passing large objects
    -> avoid passing by value, pass by reference
    • creating huge variable again, copying and transferring is too expensive
  • Use const qualifiers for arguments and functions wherever possible.
  • Don’t pass pointers when you mean to pass objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant