c++ design question: store identifiers

P

puzzlecracker

I have this problem:

I read string identifiers from the file, and based on identifier I
want to invoke certain function. So, in theory, I need something
like enum that supports strings, and then do switch that...Here is a
rough draft of what it should be:

class Value{

private std::string identifier; //read from the file

};

In code

void SomeFunction()
{

switch (VAlue.identifier)
{
case A: // do something, break
case A: // do something, break
case default: report an error error
}
}
what is the common design for this sort of a problem?
 
A

AnonMail2005

I have this problem:

I read string identifiers from the file, and based on identifier I
want to invoke certain function. So,  in  theory, I need something
like enum that supports strings, and then do switch  that...Here is a
rough draft of what it should be:

class Value{

private std::string identifier; //read from the file

};

In code

void SomeFunction()
{

     switch (VAlue.identifier)
    {
        case A: // do something, break
        case A: // do something, break
        case default: report an error error
    }}

what is the common design for this sort of a problem?

The string representation is really for external consumption - screen,
file, or database. Interanally, you want to use an enum. So the task
at hand is to create the functionality to convert a string to an
enum. This is such a commona task that you can probably find some
good solutions on the web.

HTH
 
M

mlimber

I have this problem:

I read string identifiers from the file, and based on identifier I
want to invoke certain function. So,  in  theory, I need something
like enum that supports strings, and then do switch  that...Here is a
rough draft of what it should be:

class Value{

private std::string identifier; //read from the file

};

In code

void SomeFunction()
{

     switch (VAlue.identifier)
    {
        case A: // do something, break
        case A: // do something, break
        case default: report an error error
    }}

what is the common design for this sort of a problem?

Use std::map, something like:

typedef void (*Fn)();
typedef std::map<std::string,Fn> MyMap;

MyMap myMap; // global for the sake of simplicity

void Hello() {/*...*/}
void World() {/*...*/}

void Call( const std::string& str )
{
MyMap::const_iterator it = myMap.find( "world" );
if( it != myMap.end() )
{
it->second();
}
}

int main()
{
myMap[ "hello" ] = &Hello;
myMap[ "world" ] = &World;

Call( "hello" );
Call( "world" );
}

Cheers! --M
 
M

mlimber

 void Call( const std::string& str )
 {
   MyMap::const_iterator it = myMap.find( "world" );

That should be:

const MyMap::const_iterator it = myMap.find( str );

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top