|
Boost Users : |
Subject: Re: [Boost-users] [any] Is it possible to support polymorphism in boost::any?
From: Richard Damon (Richard_at_[hidden])
Date: 2010-09-06 11:29:39
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 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