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. Why they are no Virtual Constructors.

Why they are no Virtual Constructors.

Scheduled Pinned Locked Moved C / C++ / MFC
10 Posts 9 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.
  • U Offline
    U Offline
    uday kiran janaswamy
    wrote on last edited by
    #1

    Hi all, i want to know exactly why there will not be any Virtual Constructors but There exist a Virtual Destructor. Please let me know the exact reason behind this.

    Uday kiran

    T D C 3 Replies Last reply
    0
    • U uday kiran janaswamy

      Hi all, i want to know exactly why there will not be any Virtual Constructors but There exist a Virtual Destructor. Please let me know the exact reason behind this.

      Uday kiran

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

      please search the board and some FAQs, this question has been asked bilions of times


      [VisualCalc][Flags Beginner's Guide] | [Forums Guidelines][My Best Advice]

      1 Reply Last reply
      0
      • U uday kiran janaswamy

        Hi all, i want to know exactly why there will not be any Virtual Constructors but There exist a Virtual Destructor. Please let me know the exact reason behind this.

        Uday kiran

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Does the v-table exist at the time the class' constructor is invoked? I think a virtual constructor would actually be pretty pointless since it is used for derived classes. See here for more.


        "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

        "Judge not by the eye but by the heart." - Native American Proverb

        U 1 Reply Last reply
        0
        • D David Crow

          Does the v-table exist at the time the class' constructor is invoked? I think a virtual constructor would actually be pretty pointless since it is used for derived classes. See here for more.


          "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

          "Judge not by the eye but by the heart." - Native American Proverb

          U Offline
          U Offline
          uday kiran janaswamy
          wrote on last edited by
          #4

          Thanks David, You have give a more valuable information. thank you very much.

          Uday kiran

          L 1 Reply Last reply
          0
          • U uday kiran janaswamy

            Thanks David, You have give a more valuable information. thank you very much.

            Uday kiran

            L Offline
            L Offline
            led mike
            wrote on last edited by
            #5

            uday kiran janaswamy wrote:

            You have give a more valuable information.

            He didn't give "information" to you he gave you a "pointer" to it. Typical C++ nub... not understanding pointers. :-D

            led mike

            M M 2 Replies Last reply
            0
            • L led mike

              uday kiran janaswamy wrote:

              You have give a more valuable information.

              He didn't give "information" to you he gave you a "pointer" to it. Typical C++ nub... not understanding pointers. :-D

              led mike

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              :laugh:

              "Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot? Of course you don't, no one does. It never happens. It's a dumb question... skip it."

              1 Reply Last reply
              0
              • L led mike

                uday kiran janaswamy wrote:

                You have give a more valuable information.

                He didn't give "information" to you he gave you a "pointer" to it. Typical C++ nub... not understanding pointers. :-D

                led mike

                M Offline
                M Offline
                Maximilien
                wrote on last edited by
                #7

                I think you misunderstood what he wrote. it should have been something like : You have given me a most valuable information. or something like that.


                Maximilien Lincourt Your Head A Splode - Strong Bad

                1 Reply Last reply
                0
                • U uday kiran janaswamy

                  Hi all, i want to know exactly why there will not be any Virtual Constructors but There exist a Virtual Destructor. Please let me know the exact reason behind this.

                  Uday kiran

                  C Offline
                  C Offline
                  cmk
                  wrote on last edited by
                  #8

                  When an object is created you are specifying the exact object type. It is constructed from the inside out so there is no need for a v-constructor. Once an object exists it can be pointed to by a pointer to a parent class type. Yet, the object needs to be destroyed from the outside in. Without a v-destructor destruction would start with the type of the pointer not of the actual object.

                  ...cmk Save the whales - collect the whole set

                  B 1 Reply Last reply
                  0
                  • C cmk

                    When an object is created you are specifying the exact object type. It is constructed from the inside out so there is no need for a v-constructor. Once an object exists it can be pointed to by a pointer to a parent class type. Yet, the object needs to be destroyed from the outside in. Without a v-destructor destruction would start with the type of the pointer not of the actual object.

                    ...cmk Save the whales - collect the whole set

                    B Offline
                    B Offline
                    Bram van Kampen
                    wrote on last edited by
                    #9

                    In Other words, When the First Step of construction takes place, we are constructing the Root Class, which has the V-Table of the Root Class.(No Idea yet of Your Classes' V-Table) If the language allowed you to declare a Virtual constructor, it would be in the V-Table of your final class, and only be available AFTER your class was constructed. Now that would be realy like mustard after the meal. Ofcourse someone could write a compiler which takes account of these things. The language it compiles would no longer be cpp though. The long and short of it is therefore, If you need a Virtual Constructor, you are organising your code wrong. :-O Look at your code and try again. regards

                    LateNightsInNewry

                    R 1 Reply Last reply
                    0
                    • B Bram van Kampen

                      In Other words, When the First Step of construction takes place, we are constructing the Root Class, which has the V-Table of the Root Class.(No Idea yet of Your Classes' V-Table) If the language allowed you to declare a Virtual constructor, it would be in the V-Table of your final class, and only be available AFTER your class was constructed. Now that would be realy like mustard after the meal. Ofcourse someone could write a compiler which takes account of these things. The language it compiles would no longer be cpp though. The long and short of it is therefore, If you need a Virtual Constructor, you are organising your code wrong. :-O Look at your code and try again. regards

                      LateNightsInNewry

                      R Offline
                      R Offline
                      Rajesh R Subramanian
                      wrote on last edited by
                      #10

                      LateNightsInNewry wrote:

                      The long and short of it is therefore, If you need a Virtual Constructor, you are organising your code wrong. :-O Look at your code and try again.

                      Even if you need one, C++ is not going to let you have one. That would be a compile time error blunder.


                      The greater the difficulty, the greater the glory. - Marcus Tullius Cicero

                      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