0

making random actions in a game makes it really look like real... so if a character has many capabilities like move, work, study... so in programming a function of those is called depending on some conditions. what we want is a more random and real-looking like action where no condition is there but depending on a random condition the character takes a random actions..

I thought to make actions (functions) in an array then declare a pointer to function and the program can randomly generate an index which on which the pointer to function will be assigned the corresponding function name from the array:

#include <iostream>

void Foo()   { std::cout << "Foo"    << std::endl; }
void Bar()   { std::cout << "Bar"    << std::endl; }
void FooBar(){ std::cout << "FooBar" << std::endl; }
void Baz()   { std::cout << "Baz"    << std::endl; }
void FooBaz(){ std::cout << "FooBaz" << std::endl; }


int main()
{

    void (*pFunc)();
    void* pvArray[5] = {(void*)Foo, (void*)Bar, (void*)FooBar, (void*)Baz, (void*)FooBaz};

    int choice;
    std::cout << "Which function: ";
    std::cin >> choice;
    std::cout << std::endl;

    // or random index: choice  = rand() % 5;

    pFunc = (void(*)())pvArray[choice];
    (*pFunc)();


    // or iteratley call them all:

    std::cout << "calling functions iteraely:" << std::endl;

    for(int i(0); i < 5; i++)
    {
        pFunc = (void(*)())pvArray[i];
        (*pFunc)();
    }

    std::cout << std::endl;
    return 0;
}
  • the program works fine but I only is it good or there's an alternative. every comment is welcome
0

1 Answer 1

5

There is absolutely no point in converting function pointers to void* and back. Define an array of function pointers, and use it as a normal array. The syntax for the declaration is described in this Q&A (it is for C, but the syntax remains the same in C++). The syntax for the call is a straightforward () application after the indexer [].

void (*pFunc[])() = {Foo, Bar, FooBar, Baz, FooBaz};
...
pFunc[choice]();

Demo.

Note: Although function pointers work in C++, a more flexible approach is to use std::function objects instead.

Sign up to request clarification or add additional context in comments.

3 Comments

@asblinkenlight: one last question: why i must provide the array size void (*ptrFunc[5])(); if I define it later? or I must initialize while declaration: like in your example: void (*pFunc[])() = {Foo, Bar, FooBar, Baz, FooBaz}; otherwise error: void (*pFunc[])(); ?
@Raindrop7 C++ always needs the size. You can either supply it explicitly, or let the compiler figure it out for you from the initializer.
yes it is like any array where either initialize it without telling explicitly the size or just declare it but providing the size

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.