Created
December 1, 2019 05:46
-
-
Save takiyu/162b3699e5304997dd0dc472e2bac120 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 <functional> | |
#include <iostream> | |
class Data { | |
public: | |
void set(float v_) { v = v_; } | |
float get() const { return v; } | |
void print() const { std::cout << v << std::endl; } | |
private: | |
float v = 0.f; | |
}; | |
class Float { | |
public: | |
Float(std::function<float()> func_get, | |
std::function<void(float f)> func_set) | |
: m_func_get(func_get), m_func_set(func_set) {} | |
float operator=(float rhs) { | |
m_func_set(rhs); | |
return rhs; | |
} | |
float operator+=(float rhs) { | |
float v = m_func_get() + rhs; | |
m_func_set(v); | |
return v; | |
} | |
float operator-=(float rhs) { | |
float v = m_func_get() - rhs; | |
m_func_set(v); | |
return v; | |
} | |
float operator*=(float rhs) { | |
float v = m_func_get() * rhs; | |
m_func_set(v); | |
return v; | |
} | |
float operator/=(float rhs) { | |
float v = m_func_get() / rhs; | |
m_func_set(v); | |
return v; | |
} | |
operator float() const { return m_func_get(); } | |
private: | |
std::function<float()> m_func_get; | |
std::function<void(float f)> m_func_set; | |
}; | |
int main(int argc, char const* argv[]) { | |
Data data; | |
Float f([&]() { return data.get(); }, [&](float f) { data.set(f); }); | |
data.print(); | |
f = 3.f; | |
data.print(); | |
f += 1.f; | |
data.print(); | |
f -= 10.f; | |
data.print(); | |
f = f + 1.f; | |
data.print(); | |
f = -f; | |
data.print(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment