Const references

Started by
22 comments, last by Nacho 22 years, 4 months ago
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!!
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
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
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.
quote:Original post by AdmiralBinary
...which I think would be kinda confusing...

...but is legal. The author goofed up big time.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
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!!
By the way, I am the Anonymous Poster.
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 failconst 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!
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.

HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
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
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
>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.

This topic is closed to new replies.

Advertisement