Created
April 17, 2011 00:12
-
-
Save dabroz/923634 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <math.h> | |
#include <stddef.h> | |
class Square | |
{ | |
private: | |
float _edge; | |
public: | |
float edge_getter() { | |
return _edge; | |
} | |
void edge_setter(float value) { | |
_edge = value; | |
} | |
class property_edge | |
{ | |
int padding; | |
public: | |
operator float () { return ((Square*)((char*)this - offsetof(Square, edge)))->edge_getter(); } | |
float & operator = (float value) { ((Square*)((char*)this - offsetof(Square, edge)))->edge_setter(value); }} edge; | |
float area_getter() { | |
return _edge * _edge; | |
} | |
void area_setter(float value) { | |
_edge = sqrtf(value); | |
} | |
class property_area | |
{ | |
int padding; | |
public: | |
operator float () { return ((Square*)((char*)this - offsetof(Square, area)))->area_getter(); } | |
float & operator = (float value) { ((Square*)((char*)this - offsetof(Square, area)))->area_setter(value); }} area; | |
}; | |
void print(float edge, float area) | |
{ | |
printf("edge = %f, area = %f\n", edge, area); | |
} | |
int main() | |
{ | |
Square s; | |
s.area = 25; | |
print(s.edge, s.area); | |
s.edge = 3; | |
print(s.edge, s.area); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment