C ++ unary in msdn

xiaoxiao2021-03-06  106

// unary_function.cpp // compile with: ////// Structure Used: // unary_function - allows us // to write operator functions accepting an // integer and return floats.

#include #include

Using namespace std;

/ * Derive class from unary_function in Order to use it * /

class unary_test: public unary_function {public: float value; unary_test () {value = 10.0;} unary_test (float x) {value = x;} result_type operator * (argument_type x); result_type operator- (argument_type x };

/ * You can now Easily create operator That Accept * // * an int AND return a float. * /

Unary_test :: result_type unary_test :: Operator * (unary_test :: argument_type x) {float tmp = value * (float) x; cout << "ValueAfter * IS" << TMP << endl; return value;}

Unary_test :: result_type unary_test :: Operator- (unary_test :: argument_type x) {float tmp = value - (float) x; cout << "ValueAfter minus is" << TMP << Endl; Return TMP;}

INT main (void) {unary_test item; unary_test item2 (18.0);

COUT << "begin" << Endl; cout.setf; item = item * 2; item2 = item2 - 5; return 0;} / * Output Begin ValueAfter * is 20.000000 ValueAfter Minus IS 13.000000 * /

转载请注明原文地址:https://www.9cbs.com/read-100394.html

New Post(0)