c++ - Pointer to array of class functions -


i'm looking here.

my class

leveleditor 

has functions this:

bool setsinglemast(game*, gamearea*, gamearea*, vector<iship*>*); bool setdoublemast(game*, gamearea*, gamearea*, vector<iship*>*); ... 

in main.cpp make array of pointers leveleditor object's functions. i'm doing this:

bool (*createships[2])(game*, gamearea*, gamearea*, vector<iship*>*) =  {leveledit->setsinglemast, leveledit->setdoublemast, ...}; 

but gives me error:

error c2440: 'initializing' : cannot convert 'overloaded-function' 'bool (__cdecl *)(game *,gamearea *,gamearea *,std::vector<_ty> *)' [     _ty=iship * ] none of functions name in scope match target type 

i don't know mean. can me?

you can't use ordinary function pointers point non-static member functions; need pointers-to-members instead.

bool (leveleditor::*createships[2])(game*, gamearea*, gamearea*, vector<iship*>*) = {&leveleditor::setsinglemast, &leveleditor::setdoublemast}; 

and need object or pointer call them with:

(level_editor->*createships[1])(game, area, area, ships); 

although, assuming can use c++11 (or boost), might find simpler use std::function wrap kind of callable type:

using namespace std::placeholders; std::function<bool(game*, gamearea*, gamearea*, vector<iship*>*)> createships[2] = {     std::bind(&leveleditor::setsinglemast, level_editor, _1, _2, _3, _4),     std::bind(&leveleditor::setdoublemast, level_editor, _1, _2, _3, _4) };  createships[1](game, area, area, ships); 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -