Skip to content

Instantly share code, notes, and snippets.

@dabroz
Created April 17, 2011 00:12
Show Gist options
  • Save dabroz/923634 to your computer and use it in GitHub Desktop.
Save dabroz/923634 to your computer and use it in GitHub Desktop.
#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