Microsoft KB Archive/158231

= STL Sample for the binary_function Structure =

Article ID: 158231

Article Last Modified on 11/18/2003

-

APPLIES TO


 * The Standard C++ Library
 * Microsoft Visual C++ 4.2 Enterprise Edition
 * Microsoft Visual C++ 4.2 Professional Edition

-



This article was previously published under Q158231



SUMMARY
The sample code below illustrates how to use the binary_function STL structure in Visual C++.



Prototype
template struct binary_function {    typedef _A1 first_argument_type; typedef _A2 second_argument_type; typedef _R result_type; }; NOTE: The class/parameter names in the prototype may not match the version in the header file. Some have been modified to improve readability.

Description
The binary_function class is used as a base class to allow the user to easily define binary operator functions that take data types A and B as arguments and return data type C objects.

Sample Code
//////////////////////////////////////////// // // Compile options needed: /GX // // binfunc.cpp : Illustrating the binary_function //              structure. // // Structure used: binary_function - base //                class used to create operator //                functions taking data types A //                 and B and returning data type C. // // Written by Mark Hagen // of Microsoft Product Support Services, // Copyright (c) 1996 Microsoft Corporation. // All rights reserved. ////////////////////////////////////////////

using namespace std;
 * 1) include
 * 2) include

class binary_test : public binary_function { public: float value; binary_test{value=10.0;} binary_test(float x){value=x;} result_type operator<<(second_argument_type arg2); };

binary_test::result_type binary_test::operator<<(binary_test::second_argument_type arg2) { value = ((int)value) << arg2; cout << "New value after shift is" << value << endl; return value; }

void main(void) { binary_test item;

cout << "Begin" << endl; item = item << 2; } Program Output is: Begin New value after shift is 40.0

