Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Nacho

Const references

This topic is 6031 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

Hi! I´m reading Sams Teach Yourself C++ in 21 Days 4º Edition and I´ve got a little problem. On page 298 the author explains how to overload the increment operator using this member function: const Counter& Counter::operator++() { ++itsVal; return *this; } He explained that he declared the reference to be constant to prevent statements like this ++++i. But I don´t see what´s wrong with such statements! Ah, one more thing: Can somebody explain me what´s the difference between returning a reference and returning a constant reference? When should I use each technique? Thanks in advance!!

Share this post


Link to post
Share on other sites
Advertisement
You make stuff const when you don''t want it to be changed...
I wouldn''t return a constant reference from operator++()...

Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

"What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO

Share this post


Link to post
Share on other sites
It also prevents things like
  ++i = 5;  

Which I think would be kinda confusing...

---------------

I finally got it all together...
...and then forgot where I put it.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
uummm... I think I didn´t get it quite well. I also made a test: I returned the incremented object and assigned it to the declaration of a Counter instance. Then I tried to increment the new instance and the compiler ran my program correctly. I simply don´t understand what happened. If I assign a constant reference to an instance, doesn´t that instance become constant?? Ah, can somebody answer my second question, please?? Thanks!!

Share this post


Link to post
Share on other sites
Return a constant reference if you do not wish for the receiving instance/variable to modify the original variable. Returning a constant reference from an increment operator is silly; here''s why:
int a = 5;
int &b = ++a; // assign b to incremented a
++b; // if b was assigned a const reference, this would fail
const int &c = a;
++c; // *this* is the use that should fail

Look up ''const'' in your language reference for further information.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
The author is right, it''s a matter of style, and mostly you should follow the "let it behave like an int rule".
So yes you could say that ++++i should be legal but for ints it aint and thus you shouldn''t let it be possible for you own classes either.

Share this post


Link to post
Share on other sites
quote:
Original post by DigitalDelusion
The author is right, it's a matter of style, and mostly you should follow the "let it behave like an int rule".
So yes you could say that ++++i should be legal but for ints it aint and thus you shouldn't let it be possible for you own classes either.




Try this in your favorite compiler, it works in mine...
  
#include <iostream.h>

int main(int argc, char* argv[])
{
int i=0;
++++i;
cout<<i<<endl;
return 0;
}


Magmai Kai Holmlor

"Oh, like you've never written buggy code" - Lee

"What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO

Edited by - Magmai Kai Holmlor on December 10, 2001 12:20:34 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
>If I assign a constant reference to an instance, doesn´t that instance become constant??

References and also pointers are just "handles" to an object. There is a difference between the reference or pointer being constant and the object itself being constant. The line between a reference and an object being constant is a bit blurry due to the syntax, but it does exist.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!