Can I overload the >> operator so that it will work on the next call:

 int a = 123; char* cstr; a >> cstr; 

In fact, this operator must "write" an in-value value into an array of characters (say, using the itoa() function inside an overloaded operator). In other words, I want the operator >> left to have, for example, an int , and not an istream stream. If such an implementation is possible, then tell the syntax overload. If not, explain why it is impossible. Thank.

    1 answer 1

    You cannot overload operators for fundamental types. To overload a binary operator, one of the operands must be a user-defined type: either a class or an enumeration.

    From the C ++ standard (13.3.1.2 Operators in expressions)

    1 If you’re a little girl, I’m making a statement like this?

    You could "pack" the right operand into a class and overload the operator <<, which in this case looks more natural, as follows

     class cstrClass { private: char *cstr; //... public: friend cstrClass & operator <<( cstrClass &c, int x ); //... }; //... cstrClass c; int a, b; //... c << a << b; 
    • Thanks for the advice. Tell me, why does the operator << in this case look more natural? - Shadr
    • @Shadr Because, if I'm not mistaken, you want to write a number to a character array. In this case, the character array acts as a stream. - Vlad from Moscow