How dynamic_cast works internally?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bo Yang

    How dynamic_cast works internally?

    Hi,
    I can understand static_cast, reinterpret_cas t and const_cast, they
    all work at compile time. But I can figure out how the C++'s dynamic-
    cast works? Could you please explain how for me?
    Thanks in advance!

    Regards!
    Bo

  • akshay.saidulu@gmail.com

    #2
    Re: How dynamic_cast works internally?

    On Oct 22, 1:51 pm, Bo Yang <struggl...@gma il.comwrote:
    Hi,
    I can understand static_cast, reinterpret_cas t and const_cast, they
    all work at compile time. But I can figure out how the C++'s dynamic-
    cast works? Could you please explain how for me?
    Thanks in advance!
    >
    Regards!
    Bo
    Dynamic cast adjests the offset adress of the perticul type
    accordigly .if it does't find the type it will raise the exception (in
    case of reference)or it will return null.
    Ex:
    class base{
    };

    class base1{
    };
    class derived : public base1,public base2{
    };
    deribed temp = new derived;
    base2 *p = dynamic_cast<ba se2*temp;

    it will adject the prt offset to base2 part of the temp object.

    Br,
    akshay saidulu

    Comment

    • Lance Diduck

      #3
      Re: How dynamic_cast works internally?

      On Oct 22, 4:51 am, Bo Yang <struggl...@gma il.comwrote:
      Hi,
      I can understand static_cast, reinterpret_cas t and const_cast, they
      all work at compile time. But I can figure out how the C++'s dynamic-
      cast works? Could you please explain how for me?
      Thanks in advance!
      >
      Regards!
      Bo
      Consider this case:
      class A{virtual ~A(){};};
      class B:public A{};
      A*a=new B;
      B*b =dynamic_cast<B *>(a);
      Conceptually, it is like this if is were written in C:

      void A_dtor(A* ){}
      typeinfo A_RTTI(){
      static typeinfo
      return typeinfo("A");
      }//compiler generated
      void(*)() A_vtbl[]={A_dtor,A_RTTI };
      struct A{
      A_vtbl*vptr;
      };
      A_ctor(){vptr=A _vtbl;}//compiler generated

      void B_dtor(B* ){}
      typeinfo B_RTTI(){
      static typeinfo
      return typeinfo("B");
      }//compiler generated
      void(*)(B*) B_vtbl[]={B_dtor,B_RTTI };
      struct B{
      B_vtbl*vptr;
      };
      B_ctor(){vptr=B _vtbl;}//compiler generated

      This is what A and B conceptually look like underneath the hood. Now
      when dynamic_cast is called, the compiler generates a function
      something like

      B* Dynamic_Cast(A* a ){
      if(*((a->vptr)+1)()==B_ RTTI())
      return a;
      return 0;
      }
      >From this, it is easy to extrapolate how this can be extended to work
      for references.

      You can see the reason now why dynamic_cast only works for types that
      have "virtual" somewhere, either a function or inheritance.

      Lance

      Comment

      • Greg Herlihy

        #4
        Re: How dynamic_cast works internally?

        On Oct 22, 2:20 am, Lance Diduck <lancedid...@ny c.rr.comwrote:
        On Oct 22, 4:51 am, Bo Yang <struggl...@gma il.comwrote:Hi,
        I can understand static_cast, reinterpret_cas t and const_cast, they
        all work at compile time. But I can figure out how the C++'s dynamic-
        cast works? Could you please explain how for me?
        Thanks in advance!
        >
        Regards!
        Bo
        >
        Consider this case:
        class A{virtual ~A(){};};
        class B:public A{};
        A*a=new B;
        B*b =dynamic_cast<B *>(a);. .
        B* Dynamic_Cast(A* a ){
        if(*((a->vptr)+1)()==B_ RTTI())
        return a;
        return 0;
        >
        }
        This implementation might work for typeid - because typeid has to
        confirm only that type of the object being tested - matches the
        specified type exactly.

        A dynamic_cast operator however has to perform a much more complicated
        test: whether an object (of unknown thype) is related to another type.
        And to make do with a lot less information about the classes involved
        than existed in the example above. For example:

        struct A
        {
        virtual ~A() {}
        };

        bool TestA(void *p)
        {
        assert( p != NULL);
        return dynamic_cast<A* >(p) != NULL;
        }

        Note that no derived classes of A (if any exist) are visible when this
        source file is compiled. So no matter how many large or complex this
        program's class hierarchy might be, dynamic_cast<ha s only the "A"
        class declaration with which to work here.

        So how is dynamic_cast<su pposed to figure out whether p points to a
        type that is somehow related to A? Well, given these constraints,
        there is only one way to solve this problem. dynamic_cast must search
        p's class hierarchy (essentially, discovering its layout as it goes
        along) and to continue the search until either a A class is located
        with the hierarchy - or there are no other classes left in the
        hierarchy that need to be checked.

        Greg


        Comment

        • Bo Yang

          #5
          Re: How dynamic_cast works internally?

          akshay.saidulu@ gmail.com ]:
          On Oct 22, 1:51 pm, Bo Yang <struggl...@gma il.comwrote:
          >Hi,
          > I can understand static_cast, reinterpret_cas t and const_cast, they
          >all work at compile time. But I can figure out how the C++'s dynamic-
          >cast works? Could you please explain how for me?
          >Thanks in advance!
          >>
          >Regards!
          >Bo
          >
          Dynamic cast adjests the offset adress of the perticul type
          accordigly .if it does't find the type it will raise the exception (in
          case of reference)or it will return null.
          Ex:
          class base{
          };
          >
          class base1{
          };
          class derived : public base1,public base2{
          };
          deribed temp = new derived;
          base2 *p = dynamic_cast<ba se2*temp;
          >
          it will adject the prt offset to base2 part of the temp object.
          Does it just adject the pointer without any additional check of type?

          Regards!
          Bo

          Comment

          • Bo Yang

            #6
            Re: How dynamic_cast works internally?

            Lance Diduck:
            Consider this case:
            class A{virtual ~A(){};};
            class B:public A{};
            A*a=new B;
            B*b =dynamic_cast<B *>(a);
            Conceptually, it is like this if is were written in C:
            >
            void A_dtor(A* ){}
            typeinfo A_RTTI(){
            static typeinfo
            return typeinfo("A");
            }//compiler generated
            void(*)() A_vtbl[]={A_dtor,A_RTTI };
            struct A{
            A_vtbl*vptr;
            };
            A_ctor(){vptr=A _vtbl;}//compiler generated
            >
            void B_dtor(B* ){}
            typeinfo B_RTTI(){
            static typeinfo
            return typeinfo("B");
            }//compiler generated
            void(*)(B*) B_vtbl[]={B_dtor,B_RTTI };
            struct B{
            B_vtbl*vptr;
            };
            B_ctor(){vptr=B _vtbl;}//compiler generated
            >
            This is what A and B conceptually look like underneath the hood. Now
            when dynamic_cast is called, the compiler generates a function
            something like
            >
            B* Dynamic_Cast(A* a ){
            if(*((a->vptr)+1)()==B_ RTTI())
            return a;
            return 0;
            }
            >
            Thank you for your detailed explaination. And you mean all dynamic_cast
            use RTTI inside. yes?
            >>From this, it is easy to extrapolate how this can be extended to work
            for references.
            >
            You can see the reason now why dynamic_cast only works for types that
            have "virtual" somewhere, either a function or inheritance.
            Yes, class with no virtual has no virtual table at all.

            Thanks!

            Regards!
            Bo

            Comment

            • Bo Yang

              #7
              Re: How dynamic_cast works internally?

              Greg Herlihy :
              On Oct 22, 2:20 am, Lance Diduck <lancedid...@ny c.rr.comwrote:
              >On Oct 22, 4:51 am, Bo Yang <struggl...@gma il.comwrote:Hi,
              >> I can understand static_cast, reinterpret_cas t and const_cast, they
              >>all work at compile time. But I can figure out how the C++'s dynamic-
              >>cast works? Could you please explain how for me?
              >>Thanks in advance!
              >>Regards!
              >>Bo
              >Consider this case:
              >class A{virtual ~A(){};};
              >class B:public A{};
              >A*a=new B;
              >B*b =dynamic_cast<B *>(a);. .
              >
              >B* Dynamic_Cast(A* a ){
              > if(*((a->vptr)+1)()==B_ RTTI())
              > return a;
              > return 0;
              >>
              >}
              >
              This implementation might work for typeid - because typeid has to
              confirm only that type of the object being tested - matches the
              specified type exactly.
              >
              A dynamic_cast operator however has to perform a much more complicated
              test: whether an object (of unknown thype) is related to another type.
              And to make do with a lot less information about the classes involved
              than existed in the example above. For example:
              >
              struct A
              {
              virtual ~A() {}
              };
              >
              bool TestA(void *p)
              {
              assert( p != NULL);
              return dynamic_cast<A* >(p) != NULL;
              }
              >
              Note that no derived classes of A (if any exist) are visible when this
              source file is compiled. So no matter how many large or complex this
              program's class hierarchy might be, dynamic_cast<ha s only the "A"
              class declaration with which to work here.
              >
              So how is dynamic_cast<su pposed to figure out whether p points to a
              type that is somehow related to A? Well, given these constraints,
              there is only one way to solve this problem. dynamic_cast must search
              p's class hierarchy (essentially, discovering its layout as it goes
              along) and to continue the search until either a A class is located
              with the hierarchy - or there are no other classes left in the
              hierarchy that need to be checked.
              So, how C++ go along the classes hierarchy at runtime? RTTI? Is RTTI a
              standard of C++? I think RTTI is not a standard part of C++!

              Regards!
              Bo

              Comment

              • dasjotre

                #8
                Re: How dynamic_cast works internally?

                On Oct 22, 10:20 am, Lance Diduck <lancedid...@ny c.rr.comwrote:
                You can see the reason now why dynamic_cast only works for types that
                have "virtual" somewhere, either a function or inheritance.
                only for upcast.

                downcast must work too regardless to
                virtuals:

                struct B{};
                struct D : B {};
                D d;
                B & b = dynamic_cast<B& >(d); // OK,
                B & b1 = d; // is OK too ;)

                DS


                Comment

                • Pete Becker

                  #9
                  Re: How dynamic_cast works internally?

                  On 2007-10-22 09:19:26 -0400, Bo Yang <struggleyb@gma il.comsaid:
                  >
                  So, how C++ go along the classes hierarchy at runtime? RTTI? Is RTTI a
                  standard of C++? I think RTTI is not a standard part of C++!
                  >
                  Well, yes, there is no specification of anything called RTTI in the
                  language definition. But RTTI is shorthand for the usual implementation
                  technique for dynamic_cast and for catching exceptions. The compiler
                  generates type information that's used at runtime to match types as
                  needed. Hence, RunTime Type Information.

                  --
                  Pete
                  Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
                  Standard C++ Library Extensions: a Tutorial and Reference
                  (www.petebecker.com/tr1book)

                  Comment

                  • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

                    #10
                    Re: How dynamic_cast works internally?

                    On 2007-10-22 15:19, Bo Yang wrote:
                    Greg Herlihy :
                    >On Oct 22, 2:20 am, Lance Diduck <lancedid...@ny c.rr.comwrote:
                    >>On Oct 22, 4:51 am, Bo Yang <struggl...@gma il.comwrote:Hi,
                    >>> I can understand static_cast, reinterpret_cas t and const_cast, they
                    >>>all work at compile time. But I can figure out how the C++'s dynamic-
                    >>>cast works? Could you please explain how for me?
                    >>>Thanks in advance!
                    >>>Regards!
                    >>>Bo
                    >>Consider this case:
                    >>class A{virtual ~A(){};};
                    >>class B:public A{};
                    >>A*a=new B;
                    >>B*b =dynamic_cast<B *>(a);. .
                    >>
                    >>B* Dynamic_Cast(A* a ){
                    >> if(*((a->vptr)+1)()==B_ RTTI())
                    >> return a;
                    >> return 0;
                    >>>
                    >>}
                    >>
                    >This implementation might work for typeid - because typeid has to
                    >confirm only that type of the object being tested - matches the
                    >specified type exactly.
                    >>
                    >A dynamic_cast operator however has to perform a much more complicated
                    >test: whether an object (of unknown thype) is related to another type.
                    >And to make do with a lot less information about the classes involved
                    >than existed in the example above. For example:
                    >>
                    > struct A
                    > {
                    > virtual ~A() {}
                    > };
                    >>
                    > bool TestA(void *p)
                    > {
                    > assert( p != NULL);
                    > return dynamic_cast<A* >(p) != NULL;
                    > }
                    >>
                    >Note that no derived classes of A (if any exist) are visible when this
                    >source file is compiled. So no matter how many large or complex this
                    >program's class hierarchy might be, dynamic_cast<ha s only the "A"
                    >class declaration with which to work here.
                    >>
                    >So how is dynamic_cast<su pposed to figure out whether p points to a
                    >type that is somehow related to A? Well, given these constraints,
                    >there is only one way to solve this problem. dynamic_cast must search
                    >p's class hierarchy (essentially, discovering its layout as it goes
                    >along) and to continue the search until either a A class is located
                    >with the hierarchy - or there are no other classes left in the
                    >hierarchy that need to be checked.
                    >
                    So, how C++ go along the classes hierarchy at runtime? RTTI? Is RTTI a
                    standard of C++? I think RTTI is not a standard part of C++!
                    RTTI is part of the C++ standard, do not confuse it with reflection
                    though. One way to accomplish this (though I do not think that is how it
                    is done) would be to add a second pointer (in addition to the vtable)
                    which points to a type object looking something like this:

                    class Type
                    {
                    char name[];
                    Type* super[];
                    Type* derived[];
                    };

                    That way typeid could (conceptually) be implemented something like this:

                    template<typena me T>
                    type_info typeid(T t)
                    {
                    return type_info(t->typePtr->name)
                    }

                    And using some simple tree walking algorithms you can find out if a type
                    is in the same type-hierarchy as another.

                    --
                    Erik Wikström

                    Comment

                    • dasjotre

                      #11
                      Re: How dynamic_cast works internally?

                      On Oct 22, 2:23 pm, dasjotre <dasjo...@googl email.comwrote:
                      On Oct 22, 10:20 am, Lance Diduck <lancedid...@ny c.rr.comwrote:
                      >
                      You can see the reason now why dynamic_cast only works for types that
                      have "virtual" somewhere, either a function or inheritance.
                      >
                      only for upcast.
                      >
                      ahmm, that would be 'downcast' instead
                      downcast must work too regardless to
                      and that would be 'upcast' instead
                      virtuals:
                      >
                      struct B{};
                      struct D : B {};
                      D d;
                      B & b = dynamic_cast<B& >(d); // OK,
                      B & b1 = d; // is OK too ;)
                      >
                      DS

                      Comment

                      • Bo Yang

                        #12
                        Re: How dynamic_cast works internally?

                        Erik Wikström
                        On 2007-10-22 15:19, Bo Yang wrote:
                        >Greg Herlihy :
                        >>On Oct 22, 2:20 am, Lance Diduck <lancedid...@ny c.rr.comwrote:
                        >>>On Oct 22, 4:51 am, Bo Yang <struggl...@gma il.comwrote:Hi,
                        >>>> I can understand static_cast, reinterpret_cas t and const_cast, they
                        >>>>all work at compile time. But I can figure out how the C++'s dynamic-
                        >>>>cast works? Could you please explain how for me?
                        >>>>Thanks in advance!
                        >>>>Regards!
                        >>>>Bo
                        >>>Consider this case:
                        >>>class A{virtual ~A(){};};
                        >>>class B:public A{};
                        >>>A*a=new B;
                        >>>B*b =dynamic_cast<B *>(a);. .
                        >>>B* Dynamic_Cast(A* a ){
                        >>> if(*((a->vptr)+1)()==B_ RTTI())
                        >>> return a;
                        >>> return 0;
                        >>>>
                        >>>}
                        >>This implementation might work for typeid - because typeid has to
                        >>confirm only that type of the object being tested - matches the
                        >>specified type exactly.
                        >>>
                        >>A dynamic_cast operator however has to perform a much more complicated
                        >>test: whether an object (of unknown thype) is related to another type.
                        >>And to make do with a lot less information about the classes involved
                        >>than existed in the example above. For example:
                        >>>
                        >> struct A
                        >> {
                        >> virtual ~A() {}
                        >> };
                        >>>
                        >> bool TestA(void *p)
                        >> {
                        >> assert( p != NULL);
                        >> return dynamic_cast<A* >(p) != NULL;
                        >> }
                        >>>
                        >>Note that no derived classes of A (if any exist) are visible when this
                        >>source file is compiled. So no matter how many large or complex this
                        >>program's class hierarchy might be, dynamic_cast<ha s only the "A"
                        >>class declaration with which to work here.
                        >>>
                        >>So how is dynamic_cast<su pposed to figure out whether p points to a
                        >>type that is somehow related to A? Well, given these constraints,
                        >>there is only one way to solve this problem. dynamic_cast must search
                        >>p's class hierarchy (essentially, discovering its layout as it goes
                        >>along) and to continue the search until either a A class is located
                        >>with the hierarchy - or there are no other classes left in the
                        >>hierarchy that need to be checked.
                        >So, how C++ go along the classes hierarchy at runtime? RTTI? Is RTTI a
                        >standard of C++? I think RTTI is not a standard part of C++!
                        >
                        RTTI is part of the C++ standard, do not confuse it with reflection
                        though. One way to accomplish this (though I do not think that is how it
                        is done) would be to add a second pointer (in addition to the vtable)
                        which points to a type object looking something like this:
                        >
                        class Type
                        {
                        char name[];
                        Type* super[];
                        Type* derived[];
                        };
                        >
                        That way typeid could (conceptually) be implemented something like this:
                        >
                        template<typena me T>
                        type_info typeid(T t)
                        {
                        return type_info(t->typePtr->name)
                        }
                        >
                        And using some simple tree walking algorithms you can find out if a type
                        is in the same type-hierarchy as another.
                        So, C++ type conversion will be time-consuming and space-consuming,
                        right? And maybe that is why C++ program is much more big than C's?

                        Regards!
                        Bo

                        Comment

                        • Bo Yang

                          #13
                          Re: How dynamic_cast works internally?

                          dasjotre :
                          On Oct 22, 2:23 pm, dasjotre <dasjo...@googl email.comwrote:
                          >On Oct 22, 10:20 am, Lance Diduck <lancedid...@ny c.rr.comwrote:
                          >>
                          >>You can see the reason now why dynamic_cast only works for types that
                          >>have "virtual" somewhere, either a function or inheritance.
                          >only for upcast.
                          >>
                          >
                          ahmm, that would be 'downcast' instead
                          >
                          >downcast must work too regardless to
                          >
                          and that would be 'upcast' instead
                          >
                          >virtuals:
                          >>
                          >struct B{};
                          >struct D : B {};
                          >D d;
                          >B & b = dynamic_cast<B& >(d); // OK,
                          >B & b1 = d; // is OK too ;)
                          Yeah, I know. Thanks!

                          Comment

                          • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

                            #14
                            Re: How dynamic_cast works internally?

                            On 2007-10-23 08:55, Bo Yang wrote:
                            Erik Wikström
                            >On 2007-10-22 15:19, Bo Yang wrote:
                            >>Greg Herlihy :
                            >>>On Oct 22, 2:20 am, Lance Diduck <lancedid...@ny c.rr.comwrote:
                            >>>>On Oct 22, 4:51 am, Bo Yang <struggl...@gma il.comwrote:Hi,
                            >>>>> I can understand static_cast, reinterpret_cas t and const_cast, they
                            >>>>>all work at compile time. But I can figure out how the C++'s dynamic-
                            >>>>>cast works? Could you please explain how for me?
                            >>>>>Thanks in advance!
                            >>>>>Regards!
                            >>>>>Bo
                            >>>>Consider this case:
                            >>>>class A{virtual ~A(){};};
                            >>>>class B:public A{};
                            >>>>A*a=new B;
                            >>>>B*b =dynamic_cast<B *>(a);. .
                            >>>>B* Dynamic_Cast(A* a ){
                            >>>> if(*((a->vptr)+1)()==B_ RTTI())
                            >>>> return a;
                            >>>> return 0;
                            >>>>>
                            >>>>}
                            >>>This implementation might work for typeid - because typeid has to
                            >>>confirm only that type of the object being tested - matches the
                            >>>specified type exactly.
                            >>>>
                            >>>A dynamic_cast operator however has to perform a much more complicated
                            >>>test: whether an object (of unknown thype) is related to another type.
                            >>>And to make do with a lot less information about the classes involved
                            >>>than existed in the example above. For example:
                            >>>>
                            >>> struct A
                            >>> {
                            >>> virtual ~A() {}
                            >>> };
                            >>>>
                            >>> bool TestA(void *p)
                            >>> {
                            >>> assert( p != NULL);
                            >>> return dynamic_cast<A* >(p) != NULL;
                            >>> }
                            >>>>
                            >>>Note that no derived classes of A (if any exist) are visible when this
                            >>>source file is compiled. So no matter how many large or complex this
                            >>>program's class hierarchy might be, dynamic_cast<ha s only the "A"
                            >>>class declaration with which to work here.
                            >>>>
                            >>>So how is dynamic_cast<su pposed to figure out whether p points to a
                            >>>type that is somehow related to A? Well, given these constraints,
                            >>>there is only one way to solve this problem. dynamic_cast must search
                            >>>p's class hierarchy (essentially, discovering its layout as it goes
                            >>>along) and to continue the search until either a A class is located
                            >>>with the hierarchy - or there are no other classes left in the
                            >>>hierarchy that need to be checked.
                            >>So, how C++ go along the classes hierarchy at runtime? RTTI? Is RTTI a
                            >>standard of C++? I think RTTI is not a standard part of C++!
                            >>
                            >RTTI is part of the C++ standard, do not confuse it with reflection
                            >though. One way to accomplish this (though I do not think that is how it
                            >is done) would be to add a second pointer (in addition to the vtable)
                            >which points to a type object looking something like this:
                            >>
                            >class Type
                            >{
                            > char name[];
                            > Type* super[];
                            > Type* derived[];
                            >};
                            >>
                            >That way typeid could (conceptually) be implemented something like this:
                            >>
                            >template<typen ame T>
                            >type_info typeid(T t)
                            >{
                            > return type_info(t->typePtr->name)
                            >}
                            >>
                            >And using some simple tree walking algorithms you can find out if a type
                            >is in the same type-hierarchy as another.
                            >
                            So, C++ type conversion will be time-consuming and space-consuming,
                            right? And maybe that is why C++ program is much more big than C's?
                            As I said, that was only a conceptual example, I do not know how they
                            really do it in a compiler. But you should notice that all but
                            dynamic_cast can be done at compile-time without any extra run-time
                            information, so to claim that C++ casts are more costly than C casts are
                            not really true. On the other hand, added type-safety obviously does not
                            come for free, so some extra overhead is inevitable.

                            --
                            Erik Wikström

                            Comment

                            • Bo Yang

                              #15
                              Re: How dynamic_cast works internally?

                              Erik Wikström :
                              On 2007-10-23 08:55, Bo Yang wrote:
                              >Erik Wikström
                              >>On 2007-10-22 15:19, Bo Yang wrote:
                              >>>Greg Herlihy :
                              >>>>On Oct 22, 2:20 am, Lance Diduck <lancedid...@ny c.rr.comwrote:
                              >>>>>On Oct 22, 4:51 am, Bo Yang <struggl...@gma il.comwrote:Hi,
                              >>>>>> I can understand static_cast, reinterpret_cas t and const_cast, they
                              >>>>>>all work at compile time. But I can figure out how the C++'s dynamic-
                              >>>>>>cast works? Could you please explain how for me?
                              >>>>>>Thanks in advance!
                              >>>>>>Regards !
                              >>>>>>Bo
                              >>>>>Consider this case:
                              >>>>>class A{virtual ~A(){};};
                              >>>>>class B:public A{};
                              >>>>>A*a=new B;
                              >>>>>B*b =dynamic_cast<B *>(a);. .
                              >>>>>B* Dynamic_Cast(A* a ){
                              >>>>> if(*((a->vptr)+1)()==B_ RTTI())
                              >>>>> return a;
                              >>>>> return 0;
                              >>>>>>
                              >>>>>}
                              >>>>This implementation might work for typeid - because typeid has to
                              >>>>confirm only that type of the object being tested - matches the
                              >>>>specified type exactly.
                              >>>>>
                              >>>>A dynamic_cast operator however has to perform a much more complicated
                              >>>>test: whether an object (of unknown thype) is related to another type.
                              >>>>And to make do with a lot less information about the classes involved
                              >>>>than existed in the example above. For example:
                              >>>>>
                              >>>> struct A
                              >>>> {
                              >>>> virtual ~A() {}
                              >>>> };
                              >>>>>
                              >>>> bool TestA(void *p)
                              >>>> {
                              >>>> assert( p != NULL);
                              >>>> return dynamic_cast<A* >(p) != NULL;
                              >>>> }
                              >>>>>
                              >>>>Note that no derived classes of A (if any exist) are visible when this
                              >>>>source file is compiled. So no matter how many large or complex this
                              >>>>program's class hierarchy might be, dynamic_cast<ha s only the "A"
                              >>>>class declaration with which to work here.
                              >>>>>
                              >>>>So how is dynamic_cast<su pposed to figure out whether p points to a
                              >>>>type that is somehow related to A? Well, given these constraints,
                              >>>>there is only one way to solve this problem. dynamic_cast must search
                              >>>>p's class hierarchy (essentially, discovering its layout as it goes
                              >>>>along) and to continue the search until either a A class is located
                              >>>>with the hierarchy - or there are no other classes left in the
                              >>>>hierarchy that need to be checked.
                              >>>So, how C++ go along the classes hierarchy at runtime? RTTI? Is RTTI a
                              >>>standard of C++? I think RTTI is not a standard part of C++!
                              >>RTTI is part of the C++ standard, do not confuse it with reflection
                              >>though. One way to accomplish this (though I do not think that is how it
                              >>is done) would be to add a second pointer (in addition to the vtable)
                              >>which points to a type object looking something like this:
                              >>>
                              >>class Type
                              >>{
                              >> char name[];
                              >> Type* super[];
                              >> Type* derived[];
                              >>};
                              >>>
                              >>That way typeid could (conceptually) be implemented something like this:
                              >>>
                              >>template<type name T>
                              >>type_info typeid(T t)
                              >>{
                              >> return type_info(t->typePtr->name)
                              >>}
                              >>>
                              >>And using some simple tree walking algorithms you can find out if a type
                              >>is in the same type-hierarchy as another.
                              >So, C++ type conversion will be time-consuming and space-consuming,
                              >right? And maybe that is why C++ program is much more big than C's?
                              >
                              As I said, that was only a conceptual example, I do not know how they
                              really do it in a compiler. But you should notice that all but
                              dynamic_cast can be done at compile-time without any extra run-time
                              information, so to claim that C++ casts are more costly than C casts are
                              not really true. On the other hand, added type-safety obviously does not
                              come for free, so some extra overhead is inevitable.
                              >
                              Yeah, thank you very much!

                              Regards!
                              Bo

                              Comment

                              Working...