Skip to content

Instantly share code, notes, and snippets.

@slacy
Created August 9, 2013 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slacy/6196218 to your computer and use it in GitHub Desktop.
Save slacy/6196218 to your computer and use it in GitHub Desktop.
#include <iostream>
#include "math.h"
template<int n> class Field {};
template<typename B, typename T>
class Member : public B {
public:
Member(T initial) { value_ = initial; }
T value() { return value_; }
private:
T value_;
};
template<typename T>
class Squared {
public:
static double compute(T v) { return v * v; }
};
template<typename T>
class Sqrt {
public:
static double compute(T v) { return sqrt(v); }
};
class Point :
public Member<Field<0>, int>,
public Member<Field<1>, int>
{
public:
Point(int x, int y) : Member<Field<0>, int>(x),
Member<Field<1>, int>(y) {}
int magnitude() {
return Sqrt<int>::compute(
Squared<int>::compute(Member<Field<0>,int>::value()) +
Squared<int>::compute(Member<Field<1>,int>::value()));
}
};
int main(void) {
Point p(3, 4);
std::cout << "Magnitude is " << p.magnitude() << "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment