1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
struct fun1
{
int mx, my, mz;
fun1( int x=10, int y=20, int z=30 ): mx(x), my(y), mz(z) { }
fun1& x( int x ) { mx = x; return *this; }
fun1& y( int y ) { my = y; return *this; }
fun1& z( int z ) { mz = z; return *this; }
// Use this one for functions of the type:
// void fun1( x, y, z )
~fun1()
{
std::cout << "(" << mx << ", " << my << ", " << mz << ")\n";
}
// Use this one for functions of the type:
// int fun1( x, y, z )
operator int () const
{
return mx * my + mz;
}
};
int main()
{
// print "(10, 12, 30)"
fun1().y(12);
// x = 10*74+30 = 770
// and print "(10, 74, 30)"
int x = fun1().y(74);
std::cout << x << "\n";
}
|