Im trying to add setter and getter functions to my classes.
In my mind it makes my classes look neater doing it like this.
I wanted to know what way is better, using a macro or using a template, what is faster?
#define PropertyM(type, var) \
private: \
type _##var; \
public: \
type Get##var() { return _##var; }\
void Set##var(type val) { _##var = val; }
template<typename T>
class Property
{
protected:
T m_val;
public:
inline Property() {}
inline Property(T val) { m_val = val; }
inline void Set(const T &a) { m_val = a; }
inline T Get() { return m_val; }
operator T() { return m_val; }
T &operator=(const T &a) { return m_val = a; }
};
class Test
{
public:
Test();
~Test();
Property<float> TemplateFloat;
PropertyM(float, MacroFloat)
};
Test::Test() : TemplateFloat(0.f), _MacroFloat(0.f)
{
// Just a example
if (TemplateFloat != 1.f)
TemplateFloat = 1.f;
if (TemplateFloat.Get() != 2.f)
TemplateFloat.Set(2.f);
if (GetMacroFloat() != 1.f)
SetMacroFloat(1.f);
}
Test::~Test()
{
}