Post: [C++] Question
09-16-2011, 05:21 PM #1
(adsbygoogle = window.adsbygoogle || []).push({}); Hello, I was wanting to know if there was anyway that you could use an array to get functions.

like this for example

array ={function1, function2, function3}
then just use like
array[0] that would do function one. or something.
i don't know if this is even possible.

if anyone can help or show me a better way of doing something like this. it'd be great.
09-17-2011, 12:28 AM #2
Epic?
Awe-Inspiring
Originally posted by D3ss3rtPr0ducts View Post
Hello, I was wanting to know if there was anyway that you could use an array to get functions.

like this for example

array ={function1, function2, function3}
then just use like
array[0] that would do function one. or something.
i don't know if this is even possible.

if anyone can help or show me a better way of doing something like this. it'd be great.


Read You must login or register to view this content. and You must login or register to view this content.. Google FTW!

The following 2 users say thank you to Epic? for this useful post:

Docko412, Pichu
09-17-2011, 12:34 AM #3
Ritztro
I am a Game Developer
Originally posted by D3ss3rtPr0ducts View Post
Hello, I was wanting to know if there was anyway that you could use an array to get functions.

like this for example

array ={function1, function2, function3}
then just use like
array[0] that would do function one. or something.
i don't know if this is even possible.

if anyone can help or show me a better way of doing something like this. it'd be great.


Well there are these things called function pointers and you could make an array of them and use them like so:
    #include <iostream>

using namespace std;

void PrintWord1();
void PrintWord2();
void PrintWord3();

int main()
{
void (*pfunc[3])();
pfunc[0] = &PrintWord1;
pfunc[1] = &PrintWord2;
pfunc[2] = &PrintWord3;

pfunc[0]();
pfunc[1]();
pfunc[2]();

return 0;
}

void PrintWord1()
{
cout << "Hello World-1" << endl;
}
void PrintWord2()
{
cout << "Hello World-2" << endl;
}
void PrintWord3()
{
cout << "Hello World-3" << endl;
}

The following user thanked Ritztro for this useful post:

Docko412

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo