Comparing Pointers to References

Comparing Pointers to References

P=Pointers, R=References

What is stored
P&R: Memory address

Can be re-assigned (to another address)
P: Yes
R: No

Can be null
P: Yes (use nullptr)
R: No (must be initialized)

Accessing contents
P: *ActorPtr
R: ActorRef

Accessing address
P: ActorPtr
R: &ActorRef

Changing the address
P: ActorPtr = &Actor
R: Not allowed

Changing the value
P: *ActorPtr = Actor
P: ActorRef = Actor

The & and * Symbols in Context

CopyOfActor = *ActorPtr;
ActorAddress = &Actor;

Symbol: * (dereference)
Syntax: *ActorPtr
Meaning: Contents at ActorPtr

Symbol: &
Syntax: &Actor, &ActorRef
Meaning: Address of Actor or ActroRef

Pointers or References?

Golden Rule: Use references unless you can’t.
– References are newer and safer.
– Pointers are older and provide back-compatibility.
– Pointers are more powerful but also dangerous.

ページトップへ