Why do we set pointers with the assignment = operator? Shouldn’t it just copy?

In C++ the assignment operator is a bit strange regarding pointers. I admit that C++’s pointers are very value oriented, but anyway it seems strange to point pointers to objects with the assignment operator (=).

Tester tester_val
Tester* tester_ptr

//value copy
tester_val = *tester_ptr;
//set pointer to object
tester_ptr = &tester_val;

And other members of the language family (Java, C#) have the same problem, and in fact, it is even worse with those two, as a reference and assigning it with the = operator is the default case. (But in C++ you can use value types, and in C# structs…)
I just checked and the web is filled with beginner programmers strugling with these concepts.

So, in my opininion, a language should have a separate assignment (e.g. copying) operator and a separate set pointer (or reference) operator.

Let’s see how it would look in Rae code:

If we’d have a value type object and a link (a non owning reference):

val Tester tester_val
link Tester tester_lnk

Assignment (or copy) operator

tester_lnk = tester_val //segfault because we're trying to copy by value to null.

Actually we’d need to ? it because links are optionals too. So the former shouldn’t even compile. So the correct line would be something like:

tester_lnk? = tester_val

But that will still not run, because link is null currently. 🙂

And a set reference operator could be one of these (or propose a better one in the comments!):

tester_lnk -> tester_val
tester_lnk @ tester_val
tester_lnk @= tester_val
tester_lnk -@ tester_val
tester_lnk +@ tester_val
tester_lnk =& tester_val
//Hmm. This looks familiar.
//Hey that's C++!! Like this:
tester_ptr = &tester_val

I personally think the nicest one of these is the -> as it is instantly recognizable as the “point to” -operator. The bad part is, that it looks like the C++ pointer dereference operator, but since we’re not using it anywhere else, it doesn’t matter.

Let’s look at it one more time, to see if it really looks good or not (as the code is going to get filled with these).

val Tester tester_val
link Tester tester_lnk -> tester_val

What do you think.

P.S. This change also needs another change. The compare references operator. Which is “==” (in C++, Java) and “is” in D. So we could use one of the following:

if( tester_lnk <-> another_lnk )
if( tester_lnk <=> another_lnk ) //looks too similar to <=
if( tester_lnk @ another_lnk )
if( tester_lnk is another_lnk ) //ok, but doesn't look like an operator.

P.P.S. Hmm. A counter argument to this reasoning is this:
We are already removing the already mentioned pointer dereferencing operator


, and just using the dot


We are doing this so that we could just easily change from a pointer type to a value type when needed, without having to replace all our


with dots.

So, how does changing the assignment to mean kind of like the same thing with pointers and values affect this code reuse? And the fact that we’ll be mostly using opts and links anyway, which need to be




anyway, if we want to use them. So doesn’t

//Can we get this to show:

serve the same purpose as


, but it just isn’t enforced in any way on compile time in C++???

Language design is interesting.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s