Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
i've just been reading some various documentation and forums and what not for interesting ways of doing things, and i've stumbled upon a syntax i've straight up never seen before. can someone tell me what the heck this means?

C++
std::vector <base *(*)()>  vectorArray;


i've been using vectors in many of my programs, but i've never seen something like this done before. apparently it allows you store a function call in it, and then, based on whatever iterator that function call is at, you just go:

C++
vectorArray[0] ();


and it calls whatever function you put there. This seems like an excellent way to call constructors for various derived classes, so you can just make an iterator synonymous with a certain class, shoot use a map even :P, but use it to call the constructor for a derived class and store it an a pointer to the base class, but the syntax is so foreign to me, i'm tentative as to whether it's safe to use it or not. i'd really like to :P so, in summary, my question is, what is this doing, how is it doing it, is this operation specific to vectors or general to c and c++ arrays, and is it safe to use it?
Posted
Updated 10-Oct-11 15:00pm
v8
Comments
Stefan_Lang 11-Oct-11 6:13am    
an excellent way to call constructors
Nope. Constructors are class methods and method function pointers require a different syntax. The syntax you posted only works for global functions.
FatalCatharsis 11-Oct-11 10:06am    
oh sorry, i didn't explain fully, i meant something like this :P.

template < class T> base * createT() {return new T;}
vector<base*(*) () > functionStore;

and then you just store the template function with the template argument of a specific derived class like:

functionStore.push_back(&createT<derived1>);
functionStore.push_back(&createT<derived2>);

etc, and then call them at runtime based on some argument that would be the corresponding indice, like if input is 0 then call functionStore[0] (); and create that specific object and store it in a pointer of the base class. polymorphic awesomeness! :P. i was stepping ahead of myself when stating all that :P
Ali Fakoor 12-Oct-11 7:10am    
If you want it more safe (i.e: peaceful), better to typedef the input type for vector template as
<pre>
typedef base *(*BaseCreatorFuncType)()
std::vector <BaseCreatorFuncType> vectorArray;
</pre>
See also the points 33.5 and 33.6 in http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.5
and http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.6

They are described for c++ and pointer to methods but they well apply to function pointers as well.
Philippe Mori 12-Oct-11 23:32pm    
There is no new syntax in that... That syntax would work with compiler more than 10 years old.
FatalCatharsis 13-Oct-11 10:10am    
whoa whoa, didn't mean, "new syntax to c++", i meant, "new syntax to me" :\ . I'm relatively new to programming, and i've never seen anything like this, i didn't really know what to google to find information on it. Shoot, when i first saw it, i didn't even know that a function was involved with it, i thought it was just trying to dereference a pointer to a pointer in a weird way or something stupid like that. now, after reading this, i google pointer to a function, and it's all over the place :P . So, uh, sorry if this was a stupid question.

base *(*)() - pointer to a function which returns a type 'base*'. So, vectorArray can take functions of type base* Fun() as its elements.
 
Share this answer
 
Comments
FatalCatharsis 11-Oct-11 9:32am    
and this is just something that i coulda been doing forever ago, pointers to functions?!? god, that seems so incredibly useful. Mkay, i know what i'm gonna be experimenting with for awhile :P. thanks
C++
base *(*)()

is the syntax for a pointer to a function which returns a pointer to base; so your vector will contain pointers to functions.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900