Skip to main content
added 197 characters in body
Source Link
tarzh
  • 4.5k
  • 10
  • 26

This is almost certainly why the -> operator exists in C and C++, since a->b is merely syntactic sugar for (*a).b*.

C++ doesn't have an equivalent postfix for overloaded subscript or call, but you could easily create something like a->[index] in your own language. You could also allow the operator to be chained for doublemore layers of indirection (so (**a).b == a->->b), although that may be pushing it.

Alternatively, you could make dereferencing itself postfix. The main downsides are deviation from the norm and potentially slightly more difficulty parsing (a* and a& look like the beginning of a multiplication and bitwise AND respectively, though a*., a&,, a&), a*;, and probably all other valid syntax using dereference are obviously not operations).

* ...except for this special case where a C++ class provides 2 semantically different overloads, but that kind of garbage is like #define true false

This is almost certainly why the -> operator exists in C and C++, since a->b is merely syntactic sugar for (*a).b.

C++ doesn't have an equivalent postfix for overloaded subscript or call, but you could easily create something like a->[index] in your own language. You could also allow the operator to be chained for double indirection (so (**a).b == a->->b), although that may be pushing it.

Alternatively, you could make dereferencing itself postfix. The main downsides are deviation from the norm and potentially slightly more difficulty parsing (a* and a& look like the beginning of a multiplication and bitwise AND respectively, though a*., a&,, a&), a*;, and probably all other valid syntax using dereference are obviously not operations).

This is almost certainly why the -> operator exists in C and C++, since a->b is merely syntactic sugar for (*a).b*.

C++ doesn't have an equivalent postfix for overloaded subscript or call, but you could easily create something like a->[index] in your own language. You could also allow the operator to be chained for more layers of indirection (so (**a).b == a->->b), although that may be pushing it.

Alternatively, you could make dereferencing itself postfix. The main downsides are deviation from the norm and potentially slightly more difficulty parsing (a* and a& look like the beginning of a multiplication and bitwise AND respectively, though a*., a&,, a&), a*;, and probably all other valid syntax using dereference are obviously not operations).

* ...except for this special case where a C++ class provides 2 semantically different overloads, but that kind of garbage is like #define true false

Source Link
tarzh
  • 4.5k
  • 10
  • 26

This is almost certainly why the -> operator exists in C and C++, since a->b is merely syntactic sugar for (*a).b.

C++ doesn't have an equivalent postfix for overloaded subscript or call, but you could easily create something like a->[index] in your own language. You could also allow the operator to be chained for double indirection (so (**a).b == a->->b), although that may be pushing it.

Alternatively, you could make dereferencing itself postfix. The main downsides are deviation from the norm and potentially slightly more difficulty parsing (a* and a& look like the beginning of a multiplication and bitwise AND respectively, though a*., a&,, a&), a*;, and probably all other valid syntax using dereference are obviously not operations).