Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. can I dynamically convert a base class object into a derived class object?

can I dynamically convert a base class object into a derived class object?

Scheduled Pinned Locked Moved C / C++ / MFC
c++questionlearning
21 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E ewighell

    e.g:

    class DerivedClass:BaseClass
    {
    void functionInDerivedClassOnly();
    }
    BaseClass pBaseClass = new BaseClass();
    //can the following line be excuted correctly?
    (DerivedClass*)pBaseClass->functionInDerivedClassOnly();
    delete pBaseClass;

    Thank you very much!!! ------------------- I am learning C++ and English

    S Offline
    S Offline
    Stephen Hewitt
    wrote on last edited by
    #2

    In general no. In the code above the compiler will go ahead and treat pBaseClass as a DerivedClass but unless it really is one you're asking for trouble. Steve

    E S 3 Replies Last reply
    0
    • S Stephen Hewitt

      In general no. In the code above the compiler will go ahead and treat pBaseClass as a DerivedClass but unless it really is one you're asking for trouble. Steve

      E Offline
      E Offline
      ewighell
      wrote on last edited by
      #3

      your answer is similar with my guess. but is there any way to implement the dynamical conversion from the base class object to a derived one. if not ,I would have to delete the base class object and recreate a derived one with the new operator. in this case I can't see the benefit of the inheritance, can I? Thank you very much!!! ------------------- I am learning C++ and English

      S C 2 Replies Last reply
      0
      • E ewighell

        your answer is similar with my guess. but is there any way to implement the dynamical conversion from the base class object to a derived one. if not ,I would have to delete the base class object and recreate a derived one with the new operator. in this case I can't see the benefit of the inheritance, can I? Thank you very much!!! ------------------- I am learning C++ and English

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #4

        You could write a constructor in the derived type that takes a reference to the base type. Why do you need to do this? It is not something that one finds he has to do often in OO code. Typically the situation is that you create a derived type but talk to it using a base class interface. Steve

        1 Reply Last reply
        0
        • E ewighell

          e.g:

          class DerivedClass:BaseClass
          {
          void functionInDerivedClassOnly();
          }
          BaseClass pBaseClass = new BaseClass();
          //can the following line be excuted correctly?
          (DerivedClass*)pBaseClass->functionInDerivedClassOnly();
          delete pBaseClass;

          Thank you very much!!! ------------------- I am learning C++ and English

          O Offline
          O Offline
          Owner drawn
          wrote on last edited by
          #5

          This will help you: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/express_72.asp[^]

          Jesus Loves:rose:

          --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

          1 Reply Last reply
          0
          • S Stephen Hewitt

            In general no. In the code above the compiler will go ahead and treat pBaseClass as a DerivedClass but unless it really is one you're asking for trouble. Steve

            E Offline
            E Offline
            ewighell
            wrote on last edited by
            #6

            Thank you guys, I finally understand it with your help. Only if it "is" actually the "specified type" in the memory can you convert it into the "specified type" . I have thought through and figure that maybe my problem dose be a complicated one and I can't find a short cut for it . Thank you very much, again!!! ------------------- I am learning C++ and English

            1 Reply Last reply
            0
            • E ewighell

              e.g:

              class DerivedClass:BaseClass
              {
              void functionInDerivedClassOnly();
              }
              BaseClass pBaseClass = new BaseClass();
              //can the following line be excuted correctly?
              (DerivedClass*)pBaseClass->functionInDerivedClassOnly();
              delete pBaseClass;

              Thank you very much!!! ------------------- I am learning C++ and English

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #7

              ewighell wrote:

              BaseClass pBaseClass = new BaseClass();

              replace with

              BaseClass pBaseClass = new DerivedClass();

              that's what polymorphism was designed for !


              TOXCCT >>> GEII power
              [toxcct][VisualCalc 2.20][VCalc 3.0 soon...] -- modified at 3:46 Thursday 19th January, 2006

              1 Reply Last reply
              0
              • S Stephen Hewitt

                In general no. In the code above the compiler will go ahead and treat pBaseClass as a DerivedClass but unless it really is one you're asking for trouble. Steve

                S Offline
                S Offline
                Stephen Hewitt
                wrote on last edited by
                #8

                Score: 1.8 (2 votes).

                How are the scores calculated? Obviously not an average as 3.6 (2x1.8) can't be obtained by adding 2 scores. Steve

                E T 2 Replies Last reply
                0
                • E ewighell

                  your answer is similar with my guess. but is there any way to implement the dynamical conversion from the base class object to a derived one. if not ,I would have to delete the base class object and recreate a derived one with the new operator. in this case I can't see the benefit of the inheritance, can I? Thank you very much!!! ------------------- I am learning C++ and English

                  C Offline
                  C Offline
                  Cedric Moonen
                  wrote on last edited by
                  #9

                  Maybe we can help you if you describe clearly what you need to do

                  1 Reply Last reply
                  0
                  • E ewighell

                    e.g:

                    class DerivedClass:BaseClass
                    {
                    void functionInDerivedClassOnly();
                    }
                    BaseClass pBaseClass = new BaseClass();
                    //can the following line be excuted correctly?
                    (DerivedClass*)pBaseClass->functionInDerivedClassOnly();
                    delete pBaseClass;

                    Thank you very much!!! ------------------- I am learning C++ and English

                    P Offline
                    P Offline
                    Prakash Nadar
                    wrote on last edited by
                    #10

                    just by casting the baseclass pointer to derived class pointer does not make the object that was created i.e. baseclass into a dereived class. Its just like calling Donkey a dog does not make donkey to bark ;). so you can do this. BaseClass* pBaseClass = new DerivedClass(); ((DerivedClass*)pBaseClass)->functionInDerivedClassOnly(); or use reinterpret_cast


                    -Prakash

                    T S 2 Replies Last reply
                    0
                    • P Prakash Nadar

                      just by casting the baseclass pointer to derived class pointer does not make the object that was created i.e. baseclass into a dereived class. Its just like calling Donkey a dog does not make donkey to bark ;). so you can do this. BaseClass* pBaseClass = new DerivedClass(); ((DerivedClass*)pBaseClass)->functionInDerivedClassOnly(); or use reinterpret_cast


                      -Prakash

                      T Offline
                      T Offline
                      toxcct
                      wrote on last edited by
                      #11

                      hey, you posted the same thing as i did 1 hour later :sigh: where were you ? :|


                      TOXCCT >>> GEII power
                      [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]

                      O P T 3 Replies Last reply
                      0
                      • S Stephen Hewitt

                        Score: 1.8 (2 votes).

                        How are the scores calculated? Obviously not an average as 3.6 (2x1.8) can't be obtained by adding 2 scores. Steve

                        E Offline
                        E Offline
                        ewighell
                        wrote on last edited by
                        #12

                        I have no idea what happened, either. I remember I voted for to 4 or 5. could someone in charge of this explain the problem? Thank you very much!!! ------------------- I am learning C++ and English

                        1 Reply Last reply
                        0
                        • T toxcct

                          hey, you posted the same thing as i did 1 hour later :sigh: where were you ? :|


                          TOXCCT >>> GEII power
                          [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]

                          O Offline
                          O Offline
                          Owner drawn
                          wrote on last edited by
                          #13

                          He reinterpreted it.;P

                          Jesus Loves:rose:

                          --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

                          T 1 Reply Last reply
                          0
                          • O Owner drawn

                            He reinterpreted it.;P

                            Jesus Loves:rose:

                            --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

                            T Offline
                            T Offline
                            toxcct
                            wrote on last edited by
                            #14

                            Owner drawn wrote:

                            He reinterpreted it

                            without quoting or refering to me ?! ;P not that sure...


                            TOXCCT >>> GEII power
                            [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]

                            1 Reply Last reply
                            0
                            • P Prakash Nadar

                              just by casting the baseclass pointer to derived class pointer does not make the object that was created i.e. baseclass into a dereived class. Its just like calling Donkey a dog does not make donkey to bark ;). so you can do this. BaseClass* pBaseClass = new DerivedClass(); ((DerivedClass*)pBaseClass)->functionInDerivedClassOnly(); or use reinterpret_cast


                              -Prakash

                              S Offline
                              S Offline
                              Stephen Hewitt
                              wrote on last edited by
                              #15

                              In the example above you should use static_cast<DerivedClass*>(pBaseClass). It's safer (but still not 100% safe) as it will only compile if it's possible for BaseClasss to exist which are also DerivedClasss (there is an inheritance relationship between them). Steve

                              P 1 Reply Last reply
                              0
                              • T toxcct

                                hey, you posted the same thing as i did 1 hour later :sigh: where were you ? :|


                                TOXCCT >>> GEII power
                                [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]

                                P Offline
                                P Offline
                                Prakash Nadar
                                wrote on last edited by
                                #16

                                toxcct wrote:

                                hey, you posted the same thing as i did 1 hour later

                                I did not read the other solutions ;)


                                -Prakash

                                1 Reply Last reply
                                0
                                • S Stephen Hewitt

                                  In the example above you should use static_cast<DerivedClass*>(pBaseClass). It's safer (but still not 100% safe) as it will only compile if it's possible for BaseClasss to exist which are also DerivedClasss (there is an inheritance relationship between them). Steve

                                  P Offline
                                  P Offline
                                  Prakash Nadar
                                  wrote on last edited by
                                  #17

                                  Stephen Hewitt wrote:

                                  In the example above you should use static_cast(pBaseClass)

                                  yes, correct. Thanks.


                                  -Prakash

                                  1 Reply Last reply
                                  0
                                  • T toxcct

                                    hey, you posted the same thing as i did 1 hour later :sigh: where were you ? :|


                                    TOXCCT >>> GEII power
                                    [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]

                                    T Offline
                                    T Offline
                                    ThatsAlok
                                    wrote on last edited by
                                    #18

                                    toxcct wrote:

                                    where were you ?

                                    Me, Sleeping :)

                                    "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                                    cheers, Alok Gupta VC Forum Q&A :- I/ IV

                                    T 1 Reply Last reply
                                    0
                                    • S Stephen Hewitt

                                      Score: 1.8 (2 votes).

                                      How are the scores calculated? Obviously not an average as 3.6 (2x1.8) can't be obtained by adding 2 scores. Steve

                                      T Offline
                                      T Offline
                                      toxcct
                                      wrote on last edited by
                                      #19

                                      the scores are ponderated with the level of the members... - a bronze member votes for 1 voice - a silver member votes for 2 voices - a gold member votes for 4 voices - a platinum member votes for 8 voices


                                      TOXCCT >>> GEII power
                                      [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]

                                      S 1 Reply Last reply
                                      0
                                      • T ThatsAlok

                                        toxcct wrote:

                                        where were you ?

                                        Me, Sleeping :)

                                        "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                                        cheers, Alok Gupta VC Forum Q&A :- I/ IV

                                        T Offline
                                        T Offline
                                        toxcct
                                        wrote on last edited by
                                        #20

                                        :zzz::zzz: zzzZZZZZZZzzzzz SShhhhhhh :zzz::zzz:


                                        TOXCCT >>> GEII power
                                        [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]

                                        1 Reply Last reply
                                        0
                                        • T toxcct

                                          the scores are ponderated with the level of the members... - a bronze member votes for 1 voice - a silver member votes for 2 voices - a gold member votes for 4 voices - a platinum member votes for 8 voices


                                          TOXCCT >>> GEII power
                                          [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]

                                          S Offline
                                          S Offline
                                          Stephen Hewitt
                                          wrote on last edited by
                                          #21

                                          Ahhh Steve

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups