Created
August 9, 2013 19:01
-
-
Save slacy/6196218 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <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