Why no virtual constructor in C++ ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • krishnacins
    New Member
    • Mar 2006
    • 2

    #1

    Why no virtual constructor in C++ ?

    Hi,
    I want to know about virtual constructor in C++.
    I know there is no such feature in C++. But why????
    What are the reasons of that????

    Thanks & Regards
    Krishna
  • 179004
    New Member
    • Aug 2006
    • 1

    #2
    A constructor can not be virtual because at the time when constructor is invoked the vittual table would not be available in the memory .hence we can not have virtual constructor .

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Also if you think logically about how constructors work and what the meaning/usage of a virtual function is in C++ then you will realise that a virtual constructor would be meaningless in C++.

      Declaring something virtual in C++ means that it can be overridden by a sub-class of the current class, however the constructor is called when the objected is created, at that time you can not be creating a sub-class of the class you must be creating the class so there would never be any need to declare a constructor virtual.

      Comment

      • venkataramana
        New Member
        • Apr 2007
        • 1

        #4
        Originally posted by krishnacins
        Hi,
        I want to know about virtual constructor in C++.
        I know there is no such feature in C++. But why????
        What are the reasons of that????

        Thanks & Regards
        Krishna
        When ever we are creating an object for a class using new operator, then at that point of time we should know what type of object we are creating, the new operator will invoke the constructor so if we don't know the type of the constructor then it is not possible to create that so because of this resone virtual constructor in not possible.

        Comment

        • sdprogrammer
          New Member
          • May 2007
          • 1

          #5
          Actually the virtual method table exists for each class not or the instances of the Class. Many languages do Have virtual Constructors. Delphi does for Example (I think C# does also). It allows you to call the proper constructor on a class reference.

          if it existed in C++ it would be kind of like this

          class Dog
          {
          virtual Dog();
          ....
          }
          class Poodle: public Dog
          {
          virtual Poodle();
          ...
          }

          class Rotweiler: public Dog
          {
          virtual Rotweiler();
          ...
          }
          class UglyPoodle:publ ic Poodle
          {
          virtual UglyPoodle();
          //...
          }

          typdef DogClass = class of Dog; //thats Delphi'ish notation

          Dog* DogFactory(DogC lass DogKind)
          {
          return new DogKind;
          }

          int main()
          {
          Dog* D = DogFactory(Pood le);
          }

          //this of course can be done without class references but in some cases this is much easier and more elegant. consider a complicated class hierarchy of Controls, WindowedControl s, Buttons etc. you can easily construct the right kind of Object without complicated switch statements or anything
          }

          Comment

          • jijijacob
            New Member
            • Dec 2007
            • 7

            #6
            i just wanted to know "What is virtual table?"

            Comment

            • Meetee
              Recognized Expert Contributor
              • Dec 2006
              • 928

              #7
              Originally posted by jijijacob
              i just wanted to know "What is virtual table?"
              Whenever a program has a virtual function declared, a v-table is constructed for the class. The v-table consists of addresses to the virtual functions for classes and pointers to the functions from each of the objects of the derived class. Whenever there is a function call made to the c++ virtual function, the v-table is used to resolve to the function address.

              For more search on google.

              Comment

              • jijijacob
                New Member
                • Dec 2007
                • 7

                #8
                can you tell me the creation and executing procedure for functions other than virtual functions?

                Comment

                Working...