Re: Base class returning children's data
On Jul 5, 2:54 am, "Alf P. Steinbach" <al...@start.no> wrote:
Well, then you run into problems if you allow variable data in those prop=
erties.
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!