Why there can be no virtual constructors??

This is a discussion on Why there can be no virtual constructors?? within the C++ Programming forums, part of the General Programming Boards category; Hello, Why there can be no virtual constructors?in class?What's the reason?...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    Why there can be no virtual constructors??

    Hello,

    Why there can be no virtual constructors?in class?What's the reason?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,602
    Because there's no need. When you create an object, you explicitly specify the type you're creating.
    And if you're creasing a Base, then it's not a Derived object, hence no virtual constructors are necessary.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    I think that there are use cases for virtual constructors, but that due to syntactic issues C++ does not have them.

    One use case is to clone an object of a derived type, where only the base type is known. Dewhurst in C++ Common Knowledge gives the example of a customer wanting to order the same meal as another customer. You know (at compile time) that the fellow wants a meal, but you do not know what meal he wants since neither customer has actually ordered their meals.

    The problem then, is that a copy constructor is invoked with syntax like this:
    Code:
    Meal my_meal(other_meal);
    This would result in type slicing. The workaround is to provide a clone() virtual function that returns an object of the specific derived type (i.e., using covariant return types), so we can write:
    Code:
    Meal* my_meal = other_meal.clone(); // or wrap the returned pointer in a smart pointer
    C++ FAQ Lite has an article on this solution: What is a "virtual constructor"?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,602
    I can see a reason for a virtual copy constructor, but I'm struggling to find a good reason for a virtual constructor, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual Box
    By ssharish2005 in forum Tech Board
    Replies: 3
    Last Post: 02-12-2009, 04:08 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Program with Shapes using Virtual Functions
    By goron350 in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2005, 01:42 PM
  4. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 10:14 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21