2 Replies - 223 Views - Last Post: 10 October 2012 - 05:39 PM Rate Topic: -----

#1 bean_grinder  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 8
  • Joined: 02-October 12

multiple functions inside one function

Posted 10 October 2012 - 04:03 PM

I want to "combine" multiple worker functions above main and call the boss function inside main which will execute all the worker functions. The worker functions have different ins and outs and I cannot figure out what to do. I'm pretty sure I need either to use pointers or structs, and cannot find examples of my particular question. This IS a school project and I cannot use classes.

An example of my code that works:

/////////////////// STUFF AT TOP///////////////////////
#include "stdafx.h"
#include <iostream>
using namespace std;
////////////////// FUNCTION DECLARATIONS ///////////////////
void see_key_SC ()
{
	cout << "You see a key.\n\n";
	cout << "1. Wanna ignore?\n\n";
	cout << "2. Wanna keep?\n\n";
	cout << "3. Wanna nap?\n\n";
}

int get_key_C (int min, int max)
{
	int Choice;
	do
	{
		cout << "Enter your choice:   ";
		cin >> Choice;
	}
	while (Choice < min || Choice > max );
	return ( Choice );
}

void key_results (int Choice)
{
	if (Choice ==1)
	{
		cout << "\nYou don't like keys?\n\n";
	}
	else if (Choice ==2)
	{
		cout << "\nYou win!\n\n";
	}
	else if (Choice == 3)
	{
		cout << "\nNitey Nite, you loose.\n\n";
	}
	else
	{
		cout << "\nPlease try again.\n\n";
	}
}
////////////////////// START MAIN   //////////////////////
int main(void)
{
	int Choice;
	
	see_key_SC ();
	Choice = get_key_C (1,4);
	key_results (Choice);

	system ("pause");
	return 0;
}
/////////////////// THE END  ////////////////////////


I'm trying to do something like this:

/////////////// DECLARATION ABOVE MAIN //////////

something Boss_Function (something)

{
	int Choice;
	
	see_key_SC ();
	Choice = get_key_C (1,4);
	key_results (Choice);

///////////////// MAIN  /////////////////////

int main ()
{
  Boss_Function (something);
  return (something);
}

//////////  THE END  ////////////////////


Thanks for your time.

Is This A Good Question/Topic? 0
  • +

Replies To: multiple functions inside one function

#2 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 176
  • Joined: 23-September 12

Re: multiple functions inside one function

Posted 10 October 2012 - 04:27 PM

Do you mean something like this?

#include <iostream>

template<typename TRet, typename... Args>
TRet bossFunction( TRet (&func)( Args... ), Args ... args )
{
    return func( args... );
}

int test(int a, int B)/>
{
    return a + b;
}

int
main()
{
    std::cout << bossFunction(test, 1,2) << "\n";
    return 0;
}


It's called a variadic template, and it's part of the new C++ standard, but it's probably not what you want.

This post has been edited by NathanMullenax: 10 October 2012 - 05:41 PM

Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: multiple functions inside one function

Posted 10 October 2012 - 05:38 PM

You could, alternatively, create an array of functors, and then pass that array to your boss function

example
#include <iostream>

//general worker functor
struct Worker {
   virtual int do_work() = 0;
};

//add functor
struct AddFunc: public Worker{
   int a, c;
   AddFunc(int _a, int _c) : a(_a), c(_c) {}
   int do_work() { return a+c; }
};

//abs functor
struct AbsFunc: public Worker {
   int a;
   AbsFunc(int _a) : a(_a) {}
   int do_work() { return a < 0.0 ? -1 * a : a; }
};

void do_all(Worker **workers, int n) {
   while(n--) 
      std::cout<<"Result from worker "<<n<<": "<<workers[n]->do_work()<<std::endl;
}
int main() {
   //array of worker functors
   Worker *workers[2] = {new AddFunc(1, 2), new AbsFunc(-5)};
   do_all(workers, 2);

   return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1