C++ self-learning notes(2)

1. const

The keyword const guarantees that the specified data won't be changed if the data is processed in the codes. 

EXAMPLE:

085048_xiTP_2968040.png

RESULT:

085220_JA6I_2968040.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值