Topic: IDEA: virtual member variables


Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/01/29
Raw View
Frank van Leeuwen wrote:
>
> Excuse me if there is already something like this; I have only a few months
> experience in C++, but I think there is a need for something like what I
> call 'virtual member variables'.
>
> Suppose I have a class CB with a member variable m_b, which is of type CMB:
>
> class CB {
>   class CMB m_b;
> };
>
> When I subclass CB to CS, it would be nice to be able to subclass the
> member variable as well. Example:
>
> class CS : public CB {
>   class CS m_b; // CB is the base class of CS
> };
>
> The base class methods that used m_b of type CB will then use m_b of type
> CS.  It is just like virtual methods, so you could call this 'virtual
> member variables'. The base class definition could be like:
>
> class CB {
> protected:
>   virtual class CMB m_b;
> };
>
> And the implementation could be just like virtual methods: using a pointer
> table.

virtual data members already exist in C++ !

Yes they do exist, it's not new !

Of course they is a restriction and the syntax is not the same (but
you yourself put a restriction on them: the overiding data member must
be derived from the base data member); also I assume you don't care
too much about the keyword virtual.

Only few C++ programmers know how to declare a virtual data member
of type SomeClass:

        SomeClass    a_member;
// ^^^^^^ don't write virtual here

All data members are virtual, and the restriction we were talking
about is that the overiding member (the one in the derived class) must
be derived from the base member (your idea) and also the other way
arround.

So basically, since derivation graphs cannot have closed strings
(like A derived from B derived from C derived from A - I don't know
the proper word in English), the type of the member in the derived
class must be the same as the type in the base class.

Of course, since it would be redondant, no special syntax is needed.

> An example could be a class that contains a member variable that is an
> array of objects. When I subclass this class and I would like to add an
> extra data element to the array, this normally is a problem in C++ (or am I
> missing a feature?)

I think that's what like is for in Eiffel. In C++ you use a template.

template <class OtherMembers>
class I_contain_an_array {
    protected:
        struct {
            Object  o;
            OtherMembers m;
        } array[20];
};

struct nothing_more {};

typedef I_contain_an_array<nothing_more> BaseClassInstance;

// OK but you can't add members in classes derived from DerivedClass
// or it has to be a template too
class DerivedClass : public I_contain_an_array<int> {
        int getIntNumber (int i) { return array[i].m; }
};

--

Valentin Bonnard
mailto:bonnardv@pratique.fr
http://www.pratique.fr/~bonnardv (Informations sur le C++ en Francais)
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/01/26
Raw View
Frank van Leeuwen writes:

> The base class methods that used m_b of type CB will then use m_b of type
> CS.  It is just like virtual methods, so you could call this 'virtual
> member variables'. The base class definition could be like:

class base {
 protected:
  const auto_ptr<class CB> m_b;
  CB& get_m_b() { return *m_b; }
 public:
  base() : m_b(new CB(/*...*/)) {/*...*/}
 protected:
  base(const auto_ptr<class CB>& p) : m_b(p) {/*...*/}
};

class derived : public base {
 protected:
  CS& get_m_b() { return static_cast<CS&>(*m_b); } // dynamic_cast?
 public:
  derived() : base(auto_ptr<class CS>(new CS(/*...*/))) {/*...*/}
};

You may need to use auto_ptr<class CB> instead of <class CS> in
derived::derived(), if your C++ compiler does not support template
constructors, but everything else should work fine.


> An example could be a class that contains a member variable that is an
> array of objects. When I subclass this class and I would like to add an
> extra data element to the array, this normally is a problem in C++ (or am I
> missing a feature?)

Built-in arrays and subclassing should not be mixed.  Use STL vectors
and a smart pointer implementation with semantics slightly different
of auto_ptr's, so that you can manage ownership of elements more
explicitly.  Or take your chance and use auto_ptr.  It may be easier
to implement, but you must be careful not to get copy constructors of
the contained auto_ptr's called, otherwise membership will be changed
and you may get objects destructed before you wished.

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: "Frank van Leeuwen" <fvl@iaehv.nl>
Date: 1997/01/25
Raw View
Excuse me if there is already something like this; I have only a few months
experience in C++, but I think there is a need for something like what I
call 'virtual member variables'.

Suppose I have a class CB with a member variable m_b, which is of type CMB:

class CB {
  class CMB m_b;
};

When I subclass CB to CS, it would be nice to be able to subclass the
member variable as well. Example:

class CS : public CB {
  class CS m_b; // CB is the base class of CS
};

The base class methods that used m_b of type CB will then use m_b of type
CS.  It is just like virtual methods, so you could call this 'virtual
member variables'. The base class definition could be like:

class CB {
protected:
  virtual class CMB m_b;
};

And the implementation could be just like virtual methods: using a pointer
table.

An example could be a class that contains a member variable that is an
array of objects. When I subclass this class and I would like to add an
extra data element to the array, this normally is a problem in C++ (or am I
missing a feature?)

Frank.


--------------------------------------------------------------------
Frank van Leeuwen                                       fvl@iaehv.nl
"Any advertisments mailed to me can and will be used against you..."
--------------------------------------------------------------------
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]