Advertisement

operator++

Started by February 20, 2000 08:04 PM
5 comments, last by Fredric 24 years, 7 months ago
C++: In the book that I''m reading, studying C++, the following code has confused me. // the counter class #include iostream.h typedef unsigned short int USHORT; class Counter { public: Counter(); ~Counter() {} USHORT GetItsValue() { return itsValue; } void SetItsValue(USHORT x) { itsValue = x; } void Increment() { ++itsValue; } void operator++ () { ++itsValue; } private: USHORT itsValue; }; Counter::Counter(): itsValue(0) {}; int main() { Counter i; cout << "The value of i is " << i.GetItsValue() << endl; i.Increment(); cout << "The value of i is " << i.GetItsValue() << endl; ++i; cout << "The value of i is " << i.GetItsValue() << endl; return 0; } the entire program is really basic, but what is really giving me grief is the void operator++ () { ++itsValue; } What I want to know is, what is operator? what does it, and how can it benefit this program? The author has done a reaaaaallllyyyyyy bad job on describing this, so I''ve turned to you fine people. Can anyone please help me? A syntax, or a summary of what operatoe is? thanks a lot..
3D Math- The type of mathematics that'll put hair on your chest!
in c++ you can incriment a number like this:

i = i+1

or:

i++

or:

++i

The operator in the class adds this ability to the class. You can now do this:

Counter my_counter;
my_counter++;

or:

++my_counter;

This will increment the class's member variable "itsValue" to 1. The reason that the "++" precedes the variable is so that the value is increamented before any action is taken. for example:

cout << my_counter++ << endl;

would print a "0" to the screen and then increment "itsValue" by one. However, :

cout << ++my_counter << endl;

would increment "itsValue" by one and then print a "1" to the screen. I hope this helps.

Edited by - Mike on 2/20/00 8:12:02 PM
Advertisement
OHHHHHHHHHHHHH... I get it now! Geez, thanks! It''s a little confusing, but I''ve got a hold of it now! Thanks for explaining it to me, I understand it clearly now!
3D Math- The type of mathematics that'll put hair on your chest!
OK. After seeing this code, I have to suggest -- no, DEMAND -- that you get a new C++ book. What buffoon wrote this one? operator++, among other things, should NOT be returning void. That completely defeats the purpose of overloading it. If he had correctly overloaded it to return the variable after doing the increment, you could do (for example):

cout << "New value is " << ++i << endl;

Ugh. It''s no wonder so many people think C++ is confusing, when all the people TEACHING C++ aren''t qualified to tie their own friggin shoes...

-Brian
Agreed, you really need to lose that book. Geesh, that''s bad...

~deadlinegrunt

Really? It''s really THAT bad of a practice to use operator++ in that way? Well, I''m new, but I''ll take your word for it. BUT... I don''t have any money right now to buy a new C++ book, so I''m gonna have to stick with this one until my b-day (March 13th). But, I''ll keep in mind that the author is a bafoon!
Thanks for the input.. I have his e-mail if you wanna bark at him a little bit! hehe
3D Math- The type of mathematics that'll put hair on your chest!
Advertisement
Since you need a reference, I''ve gotten a good deal of useful information from this book, which is online:
http://www.codeguru.com/cpp/tic/index.shtml
To be honest I''m not sure what his take on operator++ overloading is, but it has been useful to me as a quick reference at times.

-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~

This topic is closed to new replies.

Advertisement