A
Alessandro [AkiRoss] Re
Hello,
I'm trying to to this: all classes of a hierarchy have a property,
which is a constant object shared between each class. I'd like to have
one function for getting that object, but each class should initialize
that object once.
For example, given the property of type Property, we have that:
class Object {
public:
const Property getP() const { return _p; }
private:
static Property _p;
}, o1, o2;
Property Object::_p = Property("Object's Prop");
class Widget : public Object {
// We don't want to redeclare all here
};
Property Widget::_p = Property("Widget's Prop");
In this way, we use the code of getP which returns always a given
static variable, but the "pointed variable" changes when accessing to
a certain class:
Object *o = new Object, *w = new Widget;
o->getP() // Object's prop
w->getP() // Wdiget's prop
The reason why I'm trying to do this is that the getP member function
may change it's way to produce the result, and it must be consistent
between all derived objects, even if it changes. So I'm trying to
reduce code localizing it in just one place.
For example, assume that we have multiple properties and that getP
returns a composition of them:
class Object {
...
float getP() const;
static int p1;
static float p2;
}
one version of getP may be:
float Object::getP() const { return p1 + p2; }
but later, it may change in:
float Object::getP() const { return p1 + 100 * p2; }
and clients shouldn't never notice this change, they just have to
handle class' properties, and never touch this function.
Actually, I'm thinking about static and virtuals, but I can't find a
proper way to do this. Maybe with templates it's also feasible. Also,
please address me to any pattern which may suit this case, I'm sorry
but I don't know many patterns.
Thanks
I'm trying to to this: all classes of a hierarchy have a property,
which is a constant object shared between each class. I'd like to have
one function for getting that object, but each class should initialize
that object once.
For example, given the property of type Property, we have that:
class Object {
public:
const Property getP() const { return _p; }
private:
static Property _p;
}, o1, o2;
Property Object::_p = Property("Object's Prop");
class Widget : public Object {
// We don't want to redeclare all here
};
Property Widget::_p = Property("Widget's Prop");
In this way, we use the code of getP which returns always a given
static variable, but the "pointed variable" changes when accessing to
a certain class:
Object *o = new Object, *w = new Widget;
o->getP() // Object's prop
w->getP() // Wdiget's prop
The reason why I'm trying to do this is that the getP member function
may change it's way to produce the result, and it must be consistent
between all derived objects, even if it changes. So I'm trying to
reduce code localizing it in just one place.
For example, assume that we have multiple properties and that getP
returns a composition of them:
class Object {
...
float getP() const;
static int p1;
static float p2;
}
one version of getP may be:
float Object::getP() const { return p1 + p2; }
but later, it may change in:
float Object::getP() const { return p1 + 100 * p2; }
and clients shouldn't never notice this change, they just have to
handle class' properties, and never touch this function.
Actually, I'm thinking about static and virtuals, but I can't find a
proper way to do this. Maybe with templates it's also feasible. Also,
please address me to any pattern which may suit this case, I'm sorry
but I don't know many patterns.
Thanks