Originally Posted by
Elysia
The function pointer can be passed via the template declaration instead to save some bytes...
4 bytes overhead is not much. It's typically what a smart pointer takes (or less).
I find it acceptable between functionality and overhead/size.
I have implemented a generic property template before. like this
Code:
template<typename T, typename Class>
class property
{
typedef property self_type;
public:
property(Class& obj, void (Class::* setter)(const T&), const T& (Class::*getter)() const)
:object_(obj), setter_(setter), getter_(getter)
{}
operator T() const
{
return object_.getter_();
}
self_type& operator=(const T&t)
{
object_.setter_(t);
return *this;
}
private:
Class& object_;
void (Class::* setter_)(const T&);
const T& (Class::*getter_)() const;
};
the length setter_ and getter_ is depend on compiler and Class. one property object takes 20 bytes, a widget has 10 property objects, a program has 20 widgets, so the program needs more 4000 bytes. it's not cheap. -_-!