java:reference C++:pointer

In C++, object variables hold object values. This is different from Java, where an object variable only is a reference to an object value that is stored elsewhere. There are circumstances where the same arrangement is required in C++. In C++, a variable that can refer to an object is called a pointer. If T is any type, then T* is a pointer to an object of type T.

Just as in Java, a pointer variable can be initialized with NULL, with another pointer variable, or with a call to new.

Employee* p = NULL;

Employee* q = new Employee("Hacker, Harry", 35000);

Employee* r = q;

Actually, there is a fourth possibility. Pointers can be initialized with the address of another object, by using the & operator.

Employee boss("Morris, Melinda", 83000);

Employee* s = &boss;

This is usually not a good idea. As a rule of thumb, C++ pointers should only refer to objects allocated wth new.

So far, C++ pointers look very much like Java object variables. There is, however, an essential syntactical difference. You must apply the * operator to access the object to which a pointer points. If p is a pointer to an Employee object, then *p refers to that object.

Employee* p = . . .;

Employee boss = *p;

You also need to refer to *p when you want to execute a method or access a data field.

(*p).setSalary(91000);

The parentheses are necessary because the . operator has a higher precedence than the * operator. The designers of C found this sufficiently ugly that they provided an alternate -> operator to combine the * and . operators. The expression

p->setSalary(91000);

invokes the setSalary method on the object *p. You can simply remember to use the . operator for objects, the -> operator for pointers.

If you do not initialize a pointer, or if the pointer is NULL or refers to an object that no longer exists, then it is an error to apply the * or -> operator. Unfortunately, the C++ runtime system does not check against these errors. If you make such a mistake, your program can die a horrible death or act flaky.

In Java, these errors are not possible. You cannot have an uninitialized reference. All objects are kept alive as long as there is a reference to it. Hence you cannot have a reference to a deleted object. The runtime system checks for null references and throws a null pointer exception if a null pointer is accessed.

There is another significant difference between C++ and Java. Java has a garbage collector that automatically reclaims all objects that are no longer needed. In C++, it is the responsibility of the programmer to manage memory.

Object variables are automatically reclaimed when they go out of scope. However, objects created with new must be reclaimed manually with the delete operator.

Employee* p = new Employee("Hacker, Harry", 38000);

. . .

delete p; /* no longer need this object */

If you forget to delete an object, then you can eventually exhaust all memory. This is called a memory leak. More importantly, if you delete an object and then continue to use it, you can overwrite data that no longer belongs to you. If you overwrite any of the data fields that are used to manage the recycled storage, the allocation mechanism can malfunction and cause subtle errors that are very difficult to diagnose and fix. For this reason, it is best if you minimize the use of pointers in C++.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值