Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 824265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:04:32+00:00 2026-05-15T03:04:32+00:00

Yes i know the phrase virtual constructor makes no sense but i still see

  • 0

Yes i know the phrase “virtual constructor” makes no sense but i still see articles like this

one: http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=184

and i’v heard it mentioned as a c++ interview.

What is the general consensus?
Is a “Virtual Constructor” good practice or something to be avoided completely?

To be more more precise, can some one provide me with a real world scenario where they have had to use it, from where i stand this concept of virt. constr. is a somewhat useless invention but i could be wrong.


  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:04:33+00:00Added an answer on May 15, 2026 at 3:04 am

    All the author has done is implement prototyping and cloning. Both of which are powerful tools in the arsenal of patterns.

    You can actually do something a lot closer to “virtual constructors” through the use of the handle/body idiom:

    
    
    struct object
    {
      void f();
      // other NVI functions...
      object(...?);
      object(object const&)
      object& operator = (object const&);
      ~object();
    
    private:
      struct impl;
      impl * pimpl;
    };
    
    struct object::impl
    {
      virtual void f() = 0;
      virtual impl* clone() = 0;
      // etc...
    };
    
    struct impA : object::impl { ... };
    struct impB : object::impl { ... };
    
    object::object(...?) : pimpl(select_impl(...?)) {}
    object::object(object const& other) : pimpl(other.pimpl->clone()) {}
    
    // etc...
    

    Don’t know if anyone has declared this an Idiom but I’ve found it useful and I’m sure others have come across the same idea themselves.

    Edit:
    You use factory methods or classes when you need to request an implementation for an interface and do not want do couple your call sites to the inheritance tree behind your abstraction.

    You use prototyping (clone()) to provide generic copying of an abstraction so that you do not have to determine type in order to make that copy.

    You would use something like I just showed for a few different reasons:

    1) You wish to totally encapsulate the inheritance relation behind an abstraction. This is one method of doing so.

    2) You want to treat it as a value type at the abstract level (you’d be forced to use pointers or something otherwise)

    3) You initially had one implementation and want to add new specifications without having to change client code, which is all using the original name either in an auto declaration or heap allocation by name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Yes I know, this title isn't really helpfull but this is the exact problem.
I know the Sales pitch answer is yes to this question, but is it
First, yes i know this is a big security NONO . But the scenario
yes i know how silly and dumb this question is, but i have been
Yes I know, another HTTP_REFERER effort. I cringe when I see it. But it
Yes I know, this question has been asked MANY times but after reading all
First, yes I know about this question , but I'm looking for a bit
(Yes I know I can call Java code from Scala; but that is pointless;
I have the following code (Yes I know it's quite long winded, but I
I want to develop a web application, like an online scheduler. (Yes I know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.