www.digitalmars.com         C & C++   DMDScript  

D - virtual data?

reply Russ Lewis <russ deming-os.org> writes:
I've often wanted to do things like this:

class Foo {...};
class Foo2 : public Foo {...};
class Bar
{
protected:
  virtual Foo foo;
};
class Bar2 : public Bar
{
protected:
   Foo2 foo;
};



The idea here is that the Bar class uses the Foo class as part of its
implementation.  The Bar2 class uses a more advanced version of Bar
(that is, a child of Bar) to implement some more advanced
functionality.  I want the Foo2 object to *replace* the Foo object so
that all code written in Bar that access the Foo object will now make
the same calls on the Foo2 object.  (I can give more detailed sample
code if somebody is interested how this comes into play.)

This didn't work in C++ because the vtable was constructed iteratively;
the most base-level object would first construct its vtable and all of
its data, and you would work up from there, modifying the vtable as each
successive child is constructed.  Since D constructs the entire vtable
before any constructors are called, it would be possible for the vtable
to include pointers to virtual data members.  It should work as long as
the new class (overriding the old one) is a public child of the base
class.
Aug 22 2001
parent reply "D Man" <clockwork austin.rr.com> writes:
I wanted to do the same with enumerations.  I made a post a while back with
the info, but basically you could extend or override the set of values in an
enumerated set.

It "may" be nice to do that with data, but i would like to see a few
real-world examples of this functionality.

Cool idea, nonetheless.

"Russ Lewis" <russ deming-os.org> wrote in message
news:3B8413AE.91A954AF deming-os.org...
 I've often wanted to do things like this:

 class Foo {...};
 class Foo2 : public Foo {...};
 class Bar
 {
 protected:
   virtual Foo foo;
 };
 class Bar2 : public Bar
 {
 protected:
    Foo2 foo;
 };



 The idea here is that the Bar class uses the Foo class as part of its
 implementation.  The Bar2 class uses a more advanced version of Bar
 (that is, a child of Bar) to implement some more advanced
 functionality.  I want the Foo2 object to *replace* the Foo object so
 that all code written in Bar that access the Foo object will now make
 the same calls on the Foo2 object.  (I can give more detailed sample
 code if somebody is interested how this comes into play.)

 This didn't work in C++ because the vtable was constructed iteratively;
 the most base-level object would first construct its vtable and all of
 its data, and you would work up from there, modifying the vtable as each
 successive child is constructed.  Since D constructs the entire vtable
 before any constructors are called, it would be possible for the vtable
 to include pointers to virtual data members.  It should work as long as
 the new class (overriding the old one) is a public child of the base
 class.
Aug 23 2001
parent Russ Lewis <russ deming-os.org> writes:
D Man wrote:

 I wanted to do the same with enumerations.  I made a post a while back with
 the info, but basically you could extend or override the set of values in an
 enumerated set.

 It "may" be nice to do that with data, but i would like to see a few
 real-world examples of this functionality.
I'm trying to think of a good example without exposing internal code. :( I have a *great* example of why this is useful (that I developed as a senior project for school), but my company has not yet said that I can GPL it - until then it's company property. I'll keep thinking to see if I can find a good example w/o exposing too much. :(
Aug 23 2001