About dynamic cast and address of base and derived objects

前端 未结 4 1977
名媛妹妹
名媛妹妹 2021-01-22 11:10

I was reading through some of effective c++ and I realized I may be incorrect in my thinking along the way.

class A
{
    public:
    void laka()
    {
        c         


        
4条回答
  •  孤独总比滥情好
    2021-01-22 11:22

    The book was correct, a dynamic_cast to cv-qualified void* converts the pointer to a pointer to the most derived object pointed to by the pointer that you supply, so you get the starting address of the derived object. Both your output statements should print the same address (assuming there isn't a specific std::ostream and B* overload for operator<<) as b is the most derived object.

    There is no reason the a base class subobject can't have the same starting address as a derived object and this is what often happens in many implementations, at least for the first base class subobject in a derived class.

提交回复
热议问题