Base class returning children's data

Discussion in 'C++' started by Alessandro [AkiRoss] Re, Jul 4, 2008.

  1. 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 :)
    Alessandro [AkiRoss] Re, Jul 4, 2008
    #1
    1. Advertising

  2. On Jul 4, 11:31 pm, "Alf P. Steinbach" <> wrote:
    > In
    >
    >     float Object::getP() const { return p1 + 100 * p2; }
    >
    > should a derived class be able to override the p1 and p2?


    Yes, because each property remains equal for each class.
    Think about that properties as static constant, for example as would
    be class "name" perperty:
    Base::name == "Base"
    Derived::name == "Derived"
    Integer::name == "Integer"

    Every class has a name, and every object would like to access to its
    class name using, for example
    this->myName()
    which return the
    static const char *name
    defined for object's class.

    Thanks
    Alessandro [AkiRoss] Re, Jul 5, 2008
    #2
    1. Advertising

  3. On Jul 5, 2:54 am, "Alf P. Steinbach" <> wrote:
    > Well, then you run into problems if you allow variable data in those properties.
    >
    > But putting that aside (just don't have that), it seems that what you're after
    > is the old concept of meta-class, like, off the cuff,


    Thanks for the reply :) It gave me some ideas...

    I'd prefer to avoid meta-classes, it seems a little code bloat to me
    for such an easy (?) task. What about this? It seems to work, but...
    Is it correct and safe?

    class Base {
    // This is the function I'd like to write only once
    const int get() { return val() * 100 + 5; }
    private:
    virtual int val() {
    static const int myVal = 10;
    return myVal;
    }
    };

    class Deri: public Base {
    private:
    virtual int val() {
    static const int myVal = 20;
    return myVal;
    }
    };


    int main(int argc, char **argv) {
    Base *o1 = new Base(),
    *o2 = new Deri();

    cout << "O1 val: " << o1->get() << endl
    << "O2 val: " << o2->get() << endl;
    return EXIT_SUCCESS;
    }

    I mean, get() will always refer to polymorphic (overwritten virtual)
    val()?
    Actually I'm having some difficulties in figuring out *why* this
    works :D

    Thanks!
    Alessandro [AkiRoss] Re, Jul 5, 2008
    #3
  4. On Jul 5, 12:35 pm, "Alf P. Steinbach" <> wrote:
    > * Alessandro [AkiRoss] Re:
    >
    > > On Jul 5, 2:54 am, "Alf P. Steinbach" <> wrote:
    > >> Well, then you run into problems if you allow variable data in those properties.

    >
    > >> But putting that aside (just don't have that), it seems that what you're after
    > >> is the old concept of meta-class, like, off the cuff,

    >
    > > Thanks for the reply :) It gave me some ideas...

    >
    > > I'd prefer to avoid meta-classes, it seems a little code bloat to me
    > > for such an easy (?) task. What about this? It seems to work, but...
    > > Is it correct and safe?

    >
    > It seems to be formally correct (disclaimer: haven't compiled).
    >
    > However it's just plain ordinary direct dynamic polymorphism.


    Ok then, and thanks for the advices!
    Also thanks for the meta-class concept, it may be useful in future :)

    Bye
    Alessandro [AkiRoss] Re, Jul 5, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alf P. Steinbach
    Replies:
    6
    Views:
    541
    John Carson
    Sep 3, 2005
  2. Hicham Mouline
    Replies:
    1
    Views:
    590
    Victor Bazarov
    Apr 20, 2009
  3. nirman longjam
    Replies:
    1
    Views:
    261
    Miki Tebeka
    Nov 24, 2011
  4. Francis Hwang
    Replies:
    3
    Views:
    88
    Robert Klemme
    Nov 29, 2004
  5. Karan Rajput
    Replies:
    2
    Views:
    139
    Abinoam Jr.
    Dec 22, 2010
Loading...

Share This Page