|
Boost Users : |
Subject: Re: [Boost-users] [any] Is it possible to support polymorphism in boost::any?
From: Tang Jiang Jun (tangjiangjun_at_[hidden])
Date: 2010-09-06 11:49:41
I agree with you that void* just support polymorphism for single
inheritance.
However I think maybe it is possible to make boost::any to support full
polymorphic behavior, because, for boost::any, there still has accurate type
information when casting happens. With some tricks, maybe we can use
dynamic_cast to help boost::any to cast the internal pointer to correct
child class object.
Thanks!
Tang Jiang Jun
On Mon, Sep 6, 2010 at 5:29 PM, Richard Damon <Richard_at_[hidden]>wrote:
> On Mon, 6 Sep 2010 16:27:56 +0200, Tang Jiang Jun
> <tangjiangjun_at_[hidden]> wrote:
>
> >Is it possible to make it support this polymorphism usage? Because
> >boost::any is a type-safe replacement of void*, I hope that boost::any has
> >the same polymorphic behavior as void*.
> >
> >Regards,
> >Tang Jiang Jun
>
>
> One thing to point out is that void* doesn't fully support polymorphic
> behavior.
>
> given:
>
> class a {
> public:
> int aa;
> };
>
> class b {
> public:
> int bb;
> }
>
> class c: public a, public b {
> int cc;
> } ccc;
>
>
> int main() {
> void* ptr = &ccc;
>
> class a* ptr1 = (class a*)(ptr);
> class b* ptr2 = (class b*)(ptr);
> }
>
> My understanding is that the assignments to ptr1 and ptr2 are
> undefined behavior, and that in practice the assignment to ptr2 will
> normally be "wrong" in that ptr2 will not point to the b sub-object.
>
> any will just tell you that you are "breaking the rules" while void
> will not.
>
>
> Richard Damon
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net