Jump to content
  • Advertisement
Sign in to follow this  
MarkS

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

This topic is 1830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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. Edited by MarkS

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites

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.

Edited by orangecat

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites


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? 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!