[C++ Problem] Qusetion about operator assignment

本帖最後由 影雪仔 於 2011-10-5 14:15 編輯

I created a box object with only one member date: volume.
I want to use three operator
1) box1 > box2:    return 1 if volume of box1 > box2, else 0
2) box++ :              volume +1
3) ++box :              volume +1
  1. class box ()
  2. {
  3.         double volume;
  4.        
  5.         //all constructor

  6. };

  7. void main()
  8. {
  9.         box boxa(50);  //volume of box = 50
  10.         box boxb(25);
  11. }
複製代碼
1) box1 > box2, i added it in the main function
  1. void main()
  2. {
  3.         ......
  4.         int a = boxa > boxb;
  5. }
複製代碼
So what should i added to the class?
My concept is
boxa>boxb = boxa.operator>(boxb)
so i need to add a function : int operator> (box& x) in the class
the format is :   return type     function of operator  ( argument)
  1. int operator> (box& x)

  2.         {
  3.                 if (......)
  4.                 return 1;
  5.         }
複製代碼
is this concept correct?

2) boxa++ :              volume +1
using the same concept as in (1)
boxa++ = boxa.operator ++ ()
return type should be a box reference and there is no argument pass
so i add this to the class
  1. class box
  2. {
  3.        ........
  4.       box& operator++ ()
  5.      {
  6.            ......
  7.      }
  8. }
複製代碼
But refer to the book, the function prototype should be
  1. box operator++ (int)
複製代碼
why?
why is there argument?

3) this is similar to 2, i think when i understand 2, i can solve it

Which book do you refer to?
1) I think it is better to pass a const reference because no modification to the object.
2) Post-increment should return a value, not a reference. It is not possible to overload unary operator so a dummy, usually int, has to be passed.
3)Just a reminder, Pre-increment should return a reference.

TOP

回復 2# KamSing

The book is beginning Visual C++ 2008
1) about const reference, thanks for your remind
2) I need some time to digest

TOP

本帖最後由 影雪仔 於 2011-10-5 15:20 編輯

回覆 2# KamSing

In above example, i modified it.
Now I direct copy the program in the book.
  1. class Length
  2. {
  3.       private:
  4.             double len;

  5.       public:
  6.             Length& operator++();      //Prefix increment operator
  7.             const Length operator++ (int);    //Postfix increment operator
  8. }
複製代碼
  1. Length& Length::operator++()
  2. {
  3.             ++(this->len);
  4.             return *this;
  5. }
複製代碼
  1. const Length& Length::operator++(int)
  2. {
  3.             Length length = *this;
  4.             ++*this;
  5.             return length;
  6. }
複製代碼
I found that the prototype and the function is different, is there printing mistake?
const Length operator++ (int);
const Length& Length::operator++(int)

and you say, there should be int passed
but in fact, when we write length++
we don't provide a int
then where is the int come from

TOP

其實我都係即時search google先識得答你
http://all_work_no_play.blogspot ... rement-and-pre.html

TOP

回復 5# KamSing

I get the answer from the blog you provided.
the int argument is used to distinguish bet pre and post only.

TOP