Overloading ++ prefix / postfix operator in terms of each other?

I have a question that may have been answered more than 9000 times, but I really don’t know how to say this, that’s what I’m going to try.

In some books and C ++ textbooks, I saw that when defining your own class that has semantics of iterative value (increment), you can overload operator++for it (everything that I am going to say here, I would assume it applies to operator--). The standard way to do this is as follows:

class MyClass {
    public:
    MyClass& operator++ () {
        increment_somehow();
        return *this;
        }
    ....
    };

Where increment_somehow()well ... somehow increases the value of the object.

You can then define the postfix version operator++as follows:

MyClass operator++ (MyClass& it, int dummy) {
    MyClass copy(it);
    ++it;
    return copy;
    }

( , ), , , , operator++, , , , . , <utility> rel_ops, ( ++ ...):

class MyClass {
    public:
    bool operator== (const MyClass& that) {
        return compare_for_equality_somehow(that);
        }
    bool operator< (const MyClass& that) {
        return compare_for_lessality_somehow(that);
        }
    ....
    using namespace std::rel_ops; // operators >, >=, <=, ! are "magically" defined!
    };

( "" , - ...)

<step_ops.hpp>, std::rel_ops, Utility. Fro, , (TM). / ? , , using namespace MyLibrary::increment_operators ()?

, , , : , , ? , ++, , , boost::do_something, , .

+3
4

Boost Boost Operators. , .

/ ?

, ; .

, ?

, , . , . , , - , .

+4

, , . , . .

+1

<template T>, ++, , , , , , template .

. template http://www.cplusplus.com/doc/tutorial/templates/

0

- , , , , , StackOverflow. . , , HeadGeek . , tjm, , . , using:

namespace scope {
class MyClass {
  public:
  bool operator== (const MyClass& that) {
    return compare_for_equality_somehow(that);
    }
  bool operator< (const MyClass& that) {
    return compare_for_lessality_somehow(that);
    }
  ....
  // using namespace std::rel_ops; // that one is incorrect
  };
using std::rel_ops::operator=!; // similarly for >, >=, <=
} // end of namespace

, , , , , rel_ops -. , , , - . using , .

, .

(, , , , - - , Boost - )

0

Source: https://habr.com/ru/post/1754522/


All Articles