Converting code using traditional pointers to using ref

Let p be a pointer and r be the ref it is being changed to.

This table details common idioms that need changing to convert the pointer to a ref:



p=NULL; r.nullify();
p=new T; r=T();
p=new T(x,y,z); r=T(x,y,z);
p==NULL, p!=NULL etc r, !r etc
delete p; Remove this statement, it is superfluous
p->*m(); (*r).*m() No Member pointer dereference
p++, p+1, etc. Illegal. Consider using a container type.


Assignment and copying refs are more expensive than the equivalent pointer operations due to the reference counting mechanism. Therefore, consider using C++ references whereever possible:

void foo(const ref<int>& x); instead of void foo(ref<int> x);
{                                        {
  const ref<int>& y=...;     instead of    ref<int> y=...;

A certain amount of care must be taken if you need to declare the ref as non-const. If it is just the target that needs updating, it is fine to use a ref<T>& variable. However, if the ref itself needs updating, then use ref<T> instead.