Michiel.Salters@tomtom.com skrev:
[color=blue]
> François Fleuret wrote:[color=green]
> > Dear all,
> >
> > I fear this is a question which has been asked zillions of
> > times. However, it seems to me to be such a legitimate feature that I
> > can't imagine it is impossible.
> >
> > How can one know the size of an object in a "virtual" manner ?
> >
> > For instance, how can I write a virtual function print_size(); which
> > prints the size of *this, and which will print the correct value with
> > an object of an inherited class ?[/color]
>
> You can't, because you can't do anything useful with it. sizeof() is
> used to allocate memory for new objects (before you can even call
> print_size()) or memcpy existing objects (which you can't do if it has
> virtual functions).
>
> What legitimate use did you imagine?
>[/color]
It is difficult to find a use for that size, but perhaps some sort of
clone:
class base
{
public:
virtual ~base() = 0;
virtual int size() = 0;
virtual void clone(void* adress) = 0;
};
void silly(base* b)
{
void* clonemem = malloc(b->size());
b->clone(clonemem);
}
/Peter[color=blue]
> HTH,
> Michiel Salters[/color]