Boost logo

Boost :

From: Igor.Smirnov_at_[hidden]
Date: 2007-09-26 09:14:25


Hi,

Thanks for replies.

On Fri, 21 Sep 2007, Mathias Gaunard wrote:

> Igor.Smirnov_at_[hidden] wrote:
>
> > May be I am behind progress, but I do not know a way to make cloning
> > pointer (or that Mathias' value object, but this seems to be the same in
> > principle) to be non-intrusive. You anyway need virtual cloning function
> > in each class of the hierarchy.
>
> Boost.Any, for example, copies dynamically objects without having to
> modify their type.
>
> Here is a simplistic possible implementation for our case.
>
> template<typename T, typename D>
> T* clone_function(const T& t)
> {
> return new D(static_cast<const D&>(t));
> }
>
> template<typename T>
> class Container
> {
> template<typename D>
> Container(const D& d) : obj(new D(d)), clone(clone_function<T, D>)
> {
> }
>
> // copy constructor, operator=, etc.
>
> ~Container()
> {
> delete obj;
> }
>
> T* obj;
> T* (*clone)(const T&);
> };
>
> As I said earlier, this has a few flaws:
> - one word per object is wasted. Polymorphic objects already have one
> word that identifies their type (the pointer to the vtable)
> - it assumes that the static type of the object the container acquires
> is the same as its dynamic type.
>

Thanks, Mathias. I see now how you can bypass assumption of "each stored
type has virtiual clone function".

But should the destructor of D and T be declared as "virtual" in order to
make this work? If yes, this going to be "intrusive" detail anyway!

The second of the "flaws" you listed seems to be bad. The first is not.
Regarding the second one, is this correct to write:

class T{...}
class D: public class T {...}

D my_derived_object;
T& my_reference = my_derived_object;
template<typename T> Container( my_reference);
// and you initialize copy function for T for the object
// which actually has type D.

template<typename T> Container cnt1 = cnt;
// and you silently construct
// new object T from slice of D, which is scarcely expected.

So this may be bad problem.

Another point: I re-read your first mail with proposal and realized that I
do not understand, why you propose value object but not smart pointer? How
do you plan to access these objects and the functions of them? Through
"."? How do you provide that the correct instance of virtual function is
called, that is from type D? Why do you need this, at all? Indeed, the
notation "->" is used to indicate possible call of virtual function,
whereas "." is always understood as fixed call (if I understand correctly,
correct me if not). So when you deal with polimorphic objects, it is
better to do through (smart) pointers. Why do you want to mix this?

I also can not undesrand the code from your first mail.

Regards
   Igor


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk