Question about dereferencing syntax in C/C++...

Started by
12 comments, last by MarkS_ 10 years, 8 months ago
I have always accepted how C/C++ dreferences structs and pointers to structs until a recent thread on a new language, C-UP. The developer stated that the language doesn't use the '->' operator to dereference pointers.

This makes a lot of sense. I'm curious why the C++ standard still requires the use of '->' and '*'?

struct foo{
int i;
int *j;
};
 
foo foo1,*foo2;
int temp;

// These next lines do the exact same thing...
foo1.i = 1;
foo2->i = 1;
temp = foo1.i;
temp = foo2->i;
temp = *foo1.j;
temp = *foo2->j;
The only reason I can see for the disparity is that '->' and '*' informs the reader that a pointer is being dereferenced. Does it truly matter? All that is being done is accessing a member of a class or struct.
Advertisement


I'm curious why the C++ standard still requires this?

Because that's what C did, and changing the standard now would break literally billions of lines of code.

As to why C made the distinction in the first place, well that's another question, but I'd imagine the answer is that when C was written, yes, it truly mattered.

To be fair, there are probably still some instances where it still matters (tight loops, HPC, embedded, etc)

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

Historical C cruft. K&R C was a hell of a language.

Because that's what C did, and changing the standard now would break literally billions of lines of code.


But there's no reason they couldn't just deprecate it.

There may not be a reason for both to exist for pointers, but for class types you can overload -> to have a different meaning. Look at smart pointers and iterators for example; you use the dot to access the smart pointer/iterator, and the arrow to access the object it contains/references.

I'm always perplexed that this is seen as an issue.

I'm always perplexed that this is seen as an issue.


It isn't so much of an issue as unnecessary. It is a distinction that doesn't seem to fulfill a need.

I'm always perplexed that this is seen as an issue.

It isn't so much of an issue as unnecessary. It is a distinction that doesn't seem to fulfill a need.

I think dereferencing operators are kind of necessary in a language that has pointers. It's a useful abstraction upon which iterators, for example,were built.

If I remember correctly, in some early version of C (or some precursor) you had ty type (*p).foo instead of p->foo, so it's a kind of syntactic sugar. Whether plain dot should also work is almost a matter of taste (except for what Brother Bob said above), but I quite like being able to just read the code and see that a pointer is being dereferenced.

On the other hand, I am not so keen on having someone deprecate -> and cause me thousands of hours of busywork for no gain at all. I think one of the main reasons behind the success of C++ is that it almost never deprecates stuff. The last thing that people want is to have to grab all the code they already have written and tested and change it. There really are better things to do.


But there's no reason they couldn't just deprecate it.

Sorry, no. There are heaps of reasons they couldn't deprecate it.

For a start deprecation is more commonly done with libraries, not language elements and in order to deprecate something, you must have an alternative to replace it with.

What would you replace "->" with?

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

What would you replace "->" with?


'.'

This was just a question that I have been pondering since learning of C-UP and its features. It just seems kind of odd, but it has been made more clear since I asked. I still do not feel it is entirely necessary, but I understand why it is kept.

Unfortunately if you replace -> with . you break smart pointers. Right now you can use smart_ptr.reset() to refer to the reset() member function of the smart pointer. If you get replace -> with . then what do you do when the pointed to type also has a reset() member function?

This topic is closed to new replies.

Advertisement