C++学习过程中的一些笔记

来源:互联网 发布:琅琊榜细节 知乎 编辑:程序博客网 时间:2024/06/05 18:28

书:C++ Primer 英文版  (以下英文部分为本人学习过程中的总结笔记,中文部分为编写博客时的注释。)

关于references的注意事项:

2.3.1 P50 References

References : 

     a references is an alias.
     a reference can only bound to an object.
     a reference must be initialized. 

Define a reference :      int ival = 1024;
                                      int &refVal = ival;

Exercise 2.17

     #include <iostream>
     int main(int argc, const char * argv[])
     {

         
// insert code here...
         
int i, &ri = i;
         i = 
5;
         ri = 
10;
         
std::cout << i << " " << ri << std::endl;
         
return 0;
     }

     the result is 10 10.
        
        it means that ri is an alias of i.        意味着ri是i的一个别名

关于使用指针
2.3.2  P52 Pointers
nt i = 10;
int *p = &i; // & is a address-of operator which  is  use to get the address of the object
std::cout << *p << std::endl;//in this line, * is a dereference operator for accessing the object to which the pointer points

指针保存一个对象的地址
A pointer stores the address of an object. 

指针和引用的关键不同之处
The key difference between references and pointers is :
1. a reference is not an object and it is always bound to the object which is its initializer ,which means that it can not refer to a different object,while a pointer is an object and can point to a different object by assignment.      

0 0
原创粉丝点击