Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>All the author has done is implement prototyping and cloning. Both of which are powerful tools in the arsenal of patterns.</p> <p>You can actually do something a lot closer to "virtual constructors" through the use of the handle/body idiom:</p> <pre><code> 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... </code></pre> <p>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.</p> <p>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.</p> <p>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.</p> <p>You would use something like I just showed for a few different reasons:</p> <p>1) You wish to totally encapsulate the inheritance relation behind an abstraction. This is one method of doing so.</p> <p>2) You want to treat it as a value type at the abstract level (you'd be forced to use pointers or something otherwise)</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload