Re: Enum and string (again)
From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 06/14/04
- Next message: Randall Nagy: "Re: trying to call a virtual method from constructor of abstract object"
- Previous message: Randall Nagy: "Re: trying to call a virtual method from constructor of abstract object"
- In reply to: KPR: "Enum and string (again)"
- Next in thread: Thomas Matthews: "Re: Enum and string (again)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 14 Jun 2004 20:43:57 +1000
"KPR" <kpruppel@optonline.net> wrote in message news:40CD240C.7010709@optonline.net...
| I'm trying to take a string (either 1 or 2 characters) and use an enum
| to get an integer that I can then use as a subscript in a Totals array.
| Therefore, this hangs on converting a string to the integral constant in
| the enum list, which then lets me get my hands on a "real" integer to
| use for a subscript.
|
| I'm still baffled. I got some replies last week, and the one I tried
| implementing was from Thomas Matthews. Here's what he gave me.
|
| ---------------------------------------------------------------------------------------------------------------
| enum Meeting_Type {B,BB,C,CD,L,K,O,OD,P,S,T,To,W} mt;
| static const string meeting_names[]=
|
| {string("B"),string("BB"),string("C"),string("CD"),string("L"),string("M"),
|
| string("O"),string("OD"),string("P"),string("S"),string("T"),string("To"),
| string("W")};
| string Get_Meeting_Type_Name(Meeting_Type mt)
| {
| return meeting_names[mt];
| }
| ---------------------------------------------------------------------------------------------------------------
| DDRlist is the input vector<string>. Two sample records are "0un0745B"
| and "4hu0900To".
| This is as far as I get. No matter where or how I place the above
| function, I still get a string in return. Somehow, there has got to be
| a way to pass the string and get the Meeting_Type mt returned, but I
| just don't see it. I have tried many combinations, all no good.
|
| int main() { /* .....................*/
| int Totals[14]={0};
| for (int i=0; i <= DDRlist.size(); i++)
| if (DDRlist[i].size() == 8) {
| string Mtgcode=DDRlist[i].substr(7,1);
| ????????????
| } // end if
| }; // end main
What Thomas presented, is what he believed you were asking
for(given your description). It sounds to me like you want
the opposite ?. If so, then use something the following:
enum Meeting_Type{ B, BB, C, CD, L, K, O, OD, P, S, T, To, W };
inline int GetMeetingType( const std::string& Key )
{
std::map<std::string, Meeting_Type> MT;
MT[ "B" ] = B; MT[ "BB" ] = BB; MT[ "C" ] = C;
MT[ "CD" ] = CD; MT[ "L" ] = L; MT[ "K" ] = K;
MT[ "O" ] = O; MT[ "OD" ] = OD; MT[ "P" ] = P;
MT[ "S" ] = S; MT[ "T" ] = T; MT[ "To" ] = To;
MT[ "W" ] = W;
return MT[ Key ];
}
There is an implicit conversion from 'Meeting_Type' to 'int'
in the function return expression.
Cheers.
Chris Val
- Next message: Randall Nagy: "Re: trying to call a virtual method from constructor of abstract object"
- Previous message: Randall Nagy: "Re: trying to call a virtual method from constructor of abstract object"
- In reply to: KPR: "Enum and string (again)"
- Next in thread: Thomas Matthews: "Re: Enum and string (again)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|