#define special operator in C ++

Let's say that I want to create a special operator! + in C ++ between two objects. I would like to use! +, For example, because I think that it is much more significant than any other operator.

One of the main things I can do is to find a free, unused statement and make the replacement work with C # define:

#define !+ %
class myclass 
{
  public:
   int operator %(myclass &c)
   {
      return 3;
   }
}

So, if I write something later

a!+b 

with a and b instances of myclass, this will work.

Now, is there a way to define it instead with an operator with some function? Sort of:

#define a!+b -> a.operatorexclamativeplus(b)

This will make the translation much less dirty and allow a potentially unlimited number of these new β€œfake operators”!

+4
5

", ?"

, !+ () . .

, , [_A-Za-z][_A-Za-z0-9]* regex 1.

++ draft section

16

...
:
...
# define - new-line
# define lparen identifier-listopt) new-line -
# define lparen...) new-line-list-list
# define lparen identifier-list,...) new-line-list-list


UPDATE:
, , #define 'd , ++.

, ( ) , , . , , :

class Foo {
    enum OpState { None , PlusState , NotState };   
public:
   Foo& operator+() {
       cout << "operator+()" << endl;
       opState = PlusState;
       return *this;
   }
   Foo& operator!() {
       cout << "operator!()" << endl;
       switch(opState) {
       case PlusState:
           operatorexclamativeplus();
           break;       
       default:
           opState = NotState;
           break;       
       }
       return *this;
   }    
private:
    Foo& operatorexclamativeplus() {
       cout << "operatorexclamativeplus()" << endl;
       opState = None;
       return *this;
    }
    Foo& operatorexclamativeplus(const Foo& rhs) {
       cout << "operatorexclamativeplus(const Foo& rhs)" << endl;
       opState = None;
       return *this;
    }   
    OpState opState;
};

int main() {
    Foo x;
    Foo z = !+x;
    return 0;
}

operator+()
operator!()
operatorexclamativeplus()

. .


- (! , +), , , -, :

class Foo {
    // ...
public:
    // Provide an additional binary 'operator+()'
    friend Foo& operator+(Foo& lhs, const Foo& rhs) {
        cout << "operator+(Foo& lhs, const Foo& rhs)" << endl;
        if(lhs.opState == NotState) {
            return lhs.operatorexclamativeplus(rhs);
        }
        return lhs;
    }
    // ...
};

int main() {
    Foo x;
    Foo y;
    Foo z = y !+ x;
    return 0;
}

.


:

  • , lvalues.
  • , , , .

______________________________________________________________________________________

1) (_, __) , , , .

+10

, ++ . , , .
( , , " ?" )

a !+ b, becuase ! .
a +! b, + , ! - .
, a +! b , .

, , .

, , +! . +! . ... 1 +! 2 12.

class MyClass
{
public:
    int value;
};

class MyClassHelper
{
public:
    int value;
};

// define a unary ! operator that takes a right hand MyClass object
const MyClassHelper operator!(const MyClass right)
{
    // this operator just copies the value into a MyClassHelper object
    MyClassHelper answer;
    answer.value = right.value;
    return answer;
}

// define a binary + operator that takes a left hand MyClass object and a right hand MyClassHelper object
const MyClass operator+(const MyClass left, const MyClassHelper right)
{
    // this operator should return the value you want the +! operator to return
    MyClass answer;
    answer.value = left.value * 10 + right.value;
    return answer;
}

int main()
{
    // initialize a and b
    MyClass a, b;
    a.value = 1;
    b.value = 2;

    // this line will now compile with the +! operator
    MyClass c;
    c = a +! b;
    cout << "c = " << c.value << endl;

    return 0;
}
+1

C/++. , , .

(Objecte-C) Bjorne Stroustrup (++/cfront)? ! (), (RATFOR).

, C/++, . , :

if (something) {
  a!+b
}

:

if (something) {
  a.operatorexclamativeplus(b);
}

++ .

, . .

+1

++.

, .

0
. , , , , . , , (/) , , .

, , , , :

int x = 23 /strangeop/ 42;

strangeop - , /. -, , /, , . , , .

:

  • #define STRANGEOP /strangeop/.
  • , , .
  • strange_op(23, 43) .
  • I did not find a case when I really need this technique, so I can’t say whether it works and where its practical limitations are.
0
source

All Articles