"Pointer to Constructor"

Started by
11 comments, last by DevFred 14 years, 10 months ago
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]
Anthony Umfer
Advertisement
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.
Anthony Umfer
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 CadetUmfer
I 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.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Oh that's cheating :P

Worked, thanks
Anthony Umfer
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]
------------------------------------I always enjoy being rated up by you ...
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);

[size=1]Visit my website, rawrrawr.com

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.
Anthony Umfer
Quote:Original post by CadetUmfer
Just 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.

This topic is closed to new replies.

Advertisement