1. const
The keyword const guarantees that the specified data won't be changed if the data is processed in the codes.
EXAMPLE:
RESULT:
As you can see in the example, n is set constant. However, in the function, the input parameter (n) would be changed if the input parameter is not constant. In the result, you can see that even though n is changed to 7 during the process, n is still (or may be reset to) its initial value 5 after the processing.
When you define a pointer:
1) const int * a : this command means the value pointed by the pointor a is a constant value.
2) int * const a : this command means that the pointer itself is a constant value.
3) const int * const a : this command means both the pointer and the value pointed to are constatnt.
2. new & delete
New and delete are operations on computer's memory (dynamic memory).
commands:
1) new: pointer = new type; 2) delete: delete pointer; ......single value
or pointer = new type[number of elements]; or delete[] pointer; ...... array
So far, my understanding is about this much on this subject. It will be updated in the future when I learn more about const, new and delete during work.
( For anyone other than myself see this, I'm a Chinese guy and I'm writing in English because I'm trying to improve my English writing skills. Pardon me for that. If you see and grammatic or spelling mistake, please leave a comment)
Here is a few links to some very good references online:
http://blog.csdn.net/Eric_Jo/article/details/4138548
http://blog.csdn.net/smf0504/article/details/52311207
http://www.cplusplus.com/doc/tutorial/dynamic/
转载于:https://my.oschina.net/u/2968040/blog/994245
最后
以上就是懵懂鼠标最近收集整理的关于C++ self-learning notes(2)的全部内容,更多相关C++内容请搜索靠谱客的其他文章。
发表评论 取消回复