By Reference or By Pointer in Method Arguments
2008-11-13 15:47
99 查看
Why Use Reference/Pointer in Method Argument
To allow you to alter a data object in the calling function
To speed up a program by passing a reference instead of an
entire data object
When Should By Reference When Should Be Pointer
No modify the data,
If the data object is small, such as a built-in data type
or a small structure, pass it by value.
If the data object is an array, use a pointer because
that's your only choice. Make the pointer a pointer to const.
If the data object is a good-sized structure, use a
const pointer or a const reference to increase program
efficiency. You save the time and space needed to copy a structure or a class
design. Make the pointer or reference const.
If the data object is a class object, use a const
reference. The semantics of class design often require using a reference,
which is the main reason why C++ added this feature. Thus, the standard way to
pass class object arguments is by reference.
Does modify the data,
If the data object is a built-in data type, use a pointer.
If the data object is an array, use your only choice, a
pointer.
If the data object is a structure, use a reference or a
pointer.
If the data object is a class object, use a reference.
To allow you to alter a data object in the calling function
To speed up a program by passing a reference instead of an
entire data object
When Should By Reference When Should Be Pointer
No modify the data,
If the data object is small, such as a built-in data type
or a small structure, pass it by value.
If the data object is an array, use a pointer because
that's your only choice. Make the pointer a pointer to const.
If the data object is a good-sized structure, use a
const pointer or a const reference to increase program
efficiency. You save the time and space needed to copy a structure or a class
design. Make the pointer or reference const.
If the data object is a class object, use a const
reference. The semantics of class design often require using a reference,
which is the main reason why C++ added this feature. Thus, the standard way to
pass class object arguments is by reference.
Does modify the data,
If the data object is a built-in data type, use a pointer.
If the data object is an array, use your only choice, a
pointer.
If the data object is a structure, use a reference or a
pointer.
If the data object is a class object, use a reference.
相关文章推荐
- When do we pass arguments by reference or pointer?
- Parameter passing in Java - by reference or by value?
- You may experience an access violation when you access an STL object through a pointer or reference in a different DLL or EXE
- undefined reference to错误 和ORA-06553: PLS-306: wrong number or types of arguments in call to "OGC_X"
- Pass By Value or Pass By Reference in java
- Android studio: The number of method references in a .dex file cannot exceed 64K. Learn how to resol
- 关于Android SDK工具Lint的误报:Class referenced in the manifest, com.xxx.xxx.receiver.NetworkReceiver, was not found in the project or the libraries
- The method setStatus(int) in the type Goods is not applicable for the arguments (Integer)
- Warning: can't find referenced field/method '...' in library class ...
- AddType requires at least two arguments, a mime type followed by one or more file extensions
- only variables should be passed by reference in,原因
- call by value or reference -
- The method setPositiveButton(int, DialogInterface.OnClickListener) in the type AlertDialog.Builder is not applicable for the arguments
- java中的 by value or by reference
- The table name must be enclosed in double quotation marks or sqare bracket while accessing EXCEL by
- Teddy's Aspect Weaver Version 0.3 with Great Updating and Fixing, Especially the Implementing of Getting Runtime Method Context Info and Method Arguments in MSIL Level
- String is passed by “reference” in Java[转]
- c#中PLS-00306: wrong number or types of arguments in call to 'BORR_BOOK'
- php 报错Deprecated: Assigning the return value of new by reference is deprecated in的解决办法
- matlab12A版本中MATLAB在运行的时候出现??? Undefined function or method 'uiopen' for input arguments of type 'cha