1, the difference between the pointer and the reference: (1) non-null difference. The reference cannot point to a null value. (2) The difference of legality. Because the pointer may be empty, you need to test it to prevent it from being empty. (3)
Reference
ReferenceIt starts with another name for the object, and the reference type references another type. Define the reference type by writing the declaration Operator in the form of & D, where D is the declared variable name:
Int ivals = 1024;
In C ++, pointers and references seem to be somewhat different, but they do similar things. So what are the different things between them?
First, you must recognize a null reference. A reference must always represent an object.
If a variable is used
Pointer: An integral object that stores an address, an integer that begins with the address of the pointer, the size of the pointer type (such as a char pointer corresponding to a memory size of 1 bytes), which is called the memory of the pointer,
1. A reference is an alias for a variable that has already been defined. Must be initialized at the time of declaration of reference. A reference is closer to a const pointer, and once it is associated with a face, it is loyal to it and cannot be
First, the pointerThe pointer points to a piece of memory whose contents are the address of the referred memory. So, the pointer is the address .1, the pointer declaration and initializationTake a look at the exampleint ival = 1024;int *pi = 0;
The pointer , simply put, is to store the address value in a variable, then the variable is a pointer, the reference is to the memory address an alias, of course, the memory address has been taken by the name.pointer size , which is related to the
One: the pointer partA pointer is a variable type that declares that a pointer variable needs to be allocated a separate storage space, and the pointer can be empty "void" to indicate that it does not point to any object.The value stored in the
Plainly, a reference in C + + is a pointer. At least the bottom-level implementation is this:An example illustrates: A bit#include int main (int argc, char *argv[]) {int a = 9;int *b = &a;int &c = a;++c;*b + = 2;return 0;} 1: #include 2:3:
1, non-empty differencePointers can be assigned null values and references are not allowed2. Differences in legalityUsing a reference does not require testing its legitimacy, while the pointer should always be tested to prevent it from being empty3,
upcasting of pointers and references
In addition to the compiler can implement automatic conversion when a function is called, it can also be converted automatically when a pointer or reference assignment is made. As with function calls, neither
Original article link
Pointers and references are quite different in form, but they all seem to have the same functionality and can directly reference objects and perform direct operations on them. But when should I use pointers? When can I use
From: http://blog.csdn.net/listening_music/article/details/6921608
The following is a brief introduction:
Pointer-for a type T, T * is the pointer type pointing to T, that is, a T * type variable can save the address of a t object, type t can be
The following is an overview of what you can say in plain words:
Pointers-For a type t,t* is a pointer to T type, that is, a variable of type t* can hold the address of a T object, and type T can add some qualifiers, such as const, volatile
16.7 returning pointers and/or references from functions
When declaring a function, you must declare it by type rule and return it. Like what:
int MyFunc(); // 返回int类型 SOMETYPE MyFunc(); // 返回SOMETYPE类型 int* MyFunc(); // 返回int类型指针 SOMETYPE
The first is an interview question about type conversion, which requires that the float variable be forcibly converted to the int type and the int & output result be given.
[Cpp] int test (){Float a = 1.0f;Cout Cout Cout Cout Return 0;}Int test
1. Knowledge Point 1.1 pointer array-an array that holds pointers(1) The pointer array is essentially an array, and the pointer is the contents of the array, indicating that each element in the array is a pointer, so the array of pointers is the
int Change1 (char* name) {Name= "Alter";return 1;}int Change2 (char* &name) {Name= "Alter";return 1;}int main () {char* string= "original!";Change1 (string);coutChange2 (string);cout}
Results: original!; Alter. Change1 is a value pass in
The const modifier pointer and reference usage, for beginners C + + people are very secretive, unintelligible.
Once you know how to use it, it's not worth a sinewy. Below I give the reader one by one to understand:
Roughly speaking, it can be
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.