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. Virtual constructors...

Virtual constructors...

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
6 Posts 5 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.
  • F Offline
    F Offline
    funwithdolphin
    wrote on last edited by
    #1

    Why Virtual constructors are not allowed in C++??

    C B C 3 Replies Last reply
    0
    • F funwithdolphin

      Why Virtual constructors are not allowed in C++??

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

      Because virtual constructor are simply useless ! When constructing an object, the compiler knows exactly which object it needs to create (as opposed to the destructors). Take the following example (CChild1 and CChild2 both derive from CBase): CBase* pData = new CChild1; CBase* pData2 = new CChild2; // // ..... Code // delete pData; delete pData2; If you look at the above example, when creating pData and pData2, the compiler knows explicitly the type of the object being created (new CChild1 or new CChild2). But when you delete the object, the compiler doesn't know it explicitly, thus requiring a virtual destructor.

      1 Reply Last reply
      0
      • F funwithdolphin

        Why Virtual constructors are not allowed in C++??

        B Offline
        B Offline
        Bob Stanneveld
        wrote on last edited by
        #3

        Hello, The virtual keyword is used to say to the compiler that a pointer to an object might be a pointer to the base class instead of the class that was instantiated. This way the code looks in a virtual function pointer table for the right function to call. Since this mechanism is used to determine which member function should be called for the instance of a class, constructors cannot be virtual, since they create instances of classes. If virtual constructors would be allowed, I figure that it would never be possible to instantiate a class with a virtual constructor, once a class has been derived from it. This would be far from nice. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

        T 1 Reply Last reply
        0
        • B Bob Stanneveld

          Hello, The virtual keyword is used to say to the compiler that a pointer to an object might be a pointer to the base class instead of the class that was instantiated. This way the code looks in a virtual function pointer table for the right function to call. Since this mechanism is used to determine which member function should be called for the instance of a class, constructors cannot be virtual, since they create instances of classes. If virtual constructors would be allowed, I figure that it would never be possible to instantiate a class with a virtual constructor, once a class has been derived from it. This would be far from nice. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

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

          powerful explaination bob ;) this worthes '5' ! :rose:


          TOXCCT >>> GEII power
          [toxcct][VisualCalc]

          B 1 Reply Last reply
          0
          • F funwithdolphin

            Why Virtual constructors are not allowed in C++??

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

            Objects are created from the inside out, and destroyed from outside in.

            class A {
            public:
            A( void );
            virtual ~A( void );
            virtual void F( void );
            long DA;
            };

            class B : public A {
            B( void );
            virtual ~B( void );
            virtual void F( void );
            long DB;
            };

            When you create an instance of A or B (or any class with a vtable) an pointer is placed at the start of the object. This pointer points to the class vtable. e.g.

            A a; // [4 bytes - vtable ptr A][4 bytes - DA]
            B b; // [4 bytes - vtable ptr B][4 bytes - DA][4 bytes - DB]

            When b is created: - memory is taken from the stack to hold size B object (12 bytes) - vtable ptr is set to A vtable - A::A() is called - vtable ptr is set to B vtable - B::B() is called Setting the vtable ptr and calling the approriate constructor method can be considered an atomic action from our point of view. From the above you can also see why you can't call virtual methods from a constructor and have them resolve forward - the vtable matches the class of the current constructor. e.g.

            A::A( void ) {
            F(); // ALWAYS calls A::F(), will never call B:F()
            }

            This is part of the reason many frameworks adopt 2 part object construction: 1. create an instance, don't do anything important in constructor (init members) 2. call an Init() method that does the real construction - will resolve virtual methods properly When b is destroyed: - (vtable already set to B vtable) - B::~B() is called - vtable ptr is set to A vtable - A::~A() is called - memory is returned to stack From the above you can also see why you can't call virtual methods from a destructor and have them resolve forward - the vtable matches the class of the current destructor. e.g.

            A::~A( void ) {
            F(); // ALWAYS calls A::F(), will never call B:F()
            }

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

            1 Reply Last reply
            0
            • T toxcct

              powerful explaination bob ;) this worthes '5' ! :rose:


              TOXCCT >>> GEII power
              [toxcct][VisualCalc]

              B Offline
              B Offline
              Bob Stanneveld
              wrote on last edited by
              #6

              toxcct wrote:

              this worthes '5' !

              Thanks! :cool: Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

              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