LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-07-2008, 02:48 PM   #1
mosca
Member
 
Registered: Apr 2004
Location: Sweden
Distribution: Debian
Posts: 41

Rep: Reputation: Disabled
c++ choose a certain function depending on variable?


Hi,
I'm trying to choose between what function should be used depending on a variable. The situation is that I have some cases I, II and III and they provide different functions to my program (with the same interface). Thus I'd like to have a switch to detect which set of functions I want to use.

I thought of something like this (all in some pseudo c++)

int main()
{
...

// determine what set of functions I want to have
selector=I; // or II or III

FunctionSelector FS(selector);

...

output1=FS.function1(x);
output2=FS.function2(x,y);
}

And the function selector class would look something like

class FunctionSelector {
public:

// constructor
FunctionSelector(Selector s) {
switch(s) {
case I:
// definition of function1 and function2 for this case
double foo1(double x) { return x*x; }
double foo2(double x,double y) { return x*y; }
privfcn1=&foo1;
privfcn2=&foo2;
case II:
// similar
case III:
// similar
}

// The interface
double function1(double x){return privfcn1(x);}
double function2(double x,double y) {return privfcn2(x,y); }

private:
double (*privfcn1)(double);
double (*privfcn2)(double,double);
};

There are a bunch of issues with this... I can't declare functions in the switch/case statement for example. My main motivation for using this method is that I could really get a good overview of my code, all functions and stuff concerning a certain selector would be defined in a single place. This is my main motivation. Any other ideas how to do this?

Thanks!
 
Old 08-07-2008, 03:08 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Take a look at this site:
http://www.newty.de/fpt/index.html (formerly function-pointer.org)

ta0kira
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash how to pass value to variable in this function? babag Programming 10 04-02-2008 11:42 PM
vimrc function !./% with variable for arguments sadarax Linux - Software 0 06-22-2007 03:13 PM
segfault caused depending on how a function is called emge1 Programming 6 04-04-2007 03:48 PM
php function->query(variable) what's that? osio Programming 3 06-12-2006 06:48 AM
PHP Variable = function? wh33t Programming 2 01-22-2005 06:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration