With a built-in type...
Call-by-Value | Call-by-Value with const |
---|---|
|
|
With a built-in type...
Call-by-Reference | Call-by-Reference with const |
---|---|
|
|
With user-defined types....
Call-by-Reference | Call-by-Reference with const |
---|---|
|
|
YES !
Yes for user-defined types....it's preferred over pass-by-value
because it save potentially inefficient copying of data.
non-const argument | const argument | ||||
---|---|---|---|---|---|
reference parameter | reference to const parameter
| |
Call-by... | Built-in types | User-defined types |
---|---|---|
Value | Use call-by-value for built-in types when you want to pass a copy of the argument | Don't pass user-defined types this way. Making a copy may be inefficient. |
Const Value | Generally, don't pass built-in types this way | Don't pass user-defined types this way. Making a copy may be inefficient. |
Reference | Use call-by-reference for built-in types when your intention is that the function will change the argument being passed. | Use call-by-reference for user-defined types when your intention is that the function will change the argument being passed. |
Reference to const | Don't pass built-in types this way. Although not "wrong", there's no savings and it's not the conventional method. Use pass-by-value instead. | Use call-by-reference-to-const user-defined types when you don't want the function to change the argument. This method is preferred over pass-by-value because it accomplishes the same thing without copying. |