   "Pointer to Constructor"
  Fred has no default constructor.  With vector I can:std::vector<Fred> a(10, Fred(5,7));What is the type of the 2nd parameter?  I would like to use it like this:void Init(Fred (*construct)()) {    Fred f = construct();}Please do not mark threads 'solved' in this forum. -- jpetrie[Edited by - jpetrie on June 22, 2009 10:33:57 AM]
  You can't form a pointer to a constructor.   ;  I know.  But then how does std::vector let me specify the constructor?  It's touched on here.   ;  The type of the second parameter is "Fred". Vector takes in a Fred instance, and then copy-constructs all the items in the vector.;Quote:Original post by CadetUmferI know.  But then how does std::vector let me specify the constructor?It doesn't. std::vector lets you specify an instance, which is then copied to fill the requested number of slots.   ;  Oh that's cheating :PWorked, thanks   ;  The type of the second parameter is simply Fred ... an instance of the Fred class used as template to copy to all entries of the vector. Your init Function would look something like that:void Init(Fred **f){   *f = new Fred();}Obviously that is not what you want because it is very Fred-specific. Maybe you should try to ask for a solution of a concrete problem. I guess there is some reasoning why you think you need to get a pointer to a constructor. [smile]Edit: I should learn typing faster ... [grin]   ;  std::vector allows you to specify an instance of the class that you want copied. They don't care how you give it to them.I'd imagine that the function signiature looks something like...vector::vector(size_t size, const T& value);   ;  Yeah I have a copy-constructor, so copying an instance works fine.  Just thought vector was using some dark corner of the language to do the impossible.   ;Quote:Original post by CadetUmferJust thought vector was using some dark corner of the language to do the impossible.Not really, but there's still plenty to be learned from this technique. What the STL was designed to use, here and elsewhere, is what was later recognized as the prototype pattern, whereby the nature of construction is not building a new instance from a collection of parameters (the paradigm you initially assumed was being used) but from a prototypical instance which exists not so much to be as to instruct new copies of itself. The first lesson, one that's definitely controversial, is that it should be possible for an object to exist without that existence having any side effects. That is, just because I construct a GameObject, doesn't mean it shows up on the screen. The second lesson is similar but distinct: Convenient copying, which goes beyond "remember the Rule of Three" to scary stuff like copy-on-write and shared pointers. The third, and most interesting lesson, comes when you generalize from the prototype pattern to prototype-based programming, and the central observation is that subclassing, constructor-based parameterization, and cloning-then-modifying-the-clone are all aspects of the same thing despite C++'s efforts to treat them separately. Required reading. Really.