Thread: Why there can be no virtual constructors??

  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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    28,413
    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"?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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, 05: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, 11: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