#include <iostream>
using namespace std;
class Coor
{
public :
int x, y;
} ;
struct DataAccess
{
virtual int Get( Coor & coor) = 0 ;
virtual void Set( Coor & coor, int value) = 0 ;
} ;
struct AccessX : public DataAccess
{
virtual int Get( Coor & coor) { return coor.x ; }
virtual void Set( Coor & coor, int value) { coor.x = value; }
} ;
struct AccessY : public DataAccess
{
virtual int Get( Coor & coor) { return coor.y ; }
virtual void Set( Coor & coor, int value) { coor.y = value; }
} ;
bool algorithm( Coor & coor, DataAccess & accessor)
{
cout << accessor.Get ( coor) << endl;
accessor.Set ( coor, 100 ) ;
cout << accessor.Get ( coor) << endl;
cout << "===============" << endl;
return true ;
}
int main( )
{
Coor coor;
coor.x = 5 ;
coor.y = 10 ;
algorithm( coor, AccessX( ) ) ;
algorithm( coor, AccessY( ) ) ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY2xhc3MgQ29vcgp7CnB1YmxpYzoKICAgIGludCB4LCB5Owp9OwoKc3RydWN0IERhdGFBY2Nlc3MKewoJdmlydHVhbCBpbnQgR2V0KENvb3IgJmNvb3IpID0gMDsKCXZpcnR1YWwgdm9pZCBTZXQoQ29vciAmY29vciwgaW50IHZhbHVlKSA9IDA7Cn07CgpzdHJ1Y3QgQWNjZXNzWCA6IHB1YmxpYyBEYXRhQWNjZXNzCnsKCXZpcnR1YWwgaW50IEdldChDb29yICZjb29yKSB7IHJldHVybiBjb29yLng7IH0KCXZpcnR1YWwgdm9pZCBTZXQoQ29vciAmY29vciwgaW50IHZhbHVlKSB7IGNvb3IueCA9IHZhbHVlOyB9Cn07CgpzdHJ1Y3QgQWNjZXNzWSA6IHB1YmxpYyBEYXRhQWNjZXNzCnsKCXZpcnR1YWwgaW50IEdldChDb29yICZjb29yKSB7IHJldHVybiBjb29yLnk7IH0KCXZpcnR1YWwgdm9pZCBTZXQoQ29vciAmY29vciwgaW50IHZhbHVlKSB7IGNvb3IueSA9IHZhbHVlOyB9Cn07Cgpib29sIGFsZ29yaXRobShDb29yICZjb29yLCBEYXRhQWNjZXNzICZhY2Nlc3NvcikKewoJY291dCA8PCBhY2Nlc3Nvci5HZXQoY29vcikgPDwgZW5kbDsKCWFjY2Vzc29yLlNldChjb29yLCAxMDApOwoJY291dCA8PCBhY2Nlc3Nvci5HZXQoY29vcikgPDwgZW5kbDsKCWNvdXQgPDwgIj09PT09PT09PT09PT09PSIgPDwgZW5kbDsKCglyZXR1cm4gdHJ1ZTsKfQoKaW50IG1haW4oKQp7CglDb29yIGNvb3I7Cgljb29yLnggPSA1OwoJY29vci55ID0gMTA7CgoJYWxnb3JpdGhtKGNvb3IsIEFjY2Vzc1goKSk7CglhbGdvcml0aG0oY29vciwgQWNjZXNzWSgpKTsKfQ==
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:44: error: invalid initialization of non-const reference of type ‘DataAccess&’ from a temporary of type ‘AccessX’
prog.cpp:28: error: in passing argument 2 of ‘bool algorithm(Coor&, DataAccess&)’
prog.cpp:45: error: invalid initialization of non-const reference of type ‘DataAccess&’ from a temporary of type ‘AccessY’
prog.cpp:28: error: in passing argument 2 of ‘bool algorithm(Coor&, DataAccess&)’
stdout