Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Here is a tricky I am stuck at.

Status
Not open for further replies.

DarkAlchemist

Programmer
Jan 19, 2004
18
US
I have 2 strings like so

a = Focus
b = Coordination

now I need to call a function like so
m_pCharStats->GetSkill(DecalFilters::eAttributeFocus)
and
m_pCharStats->GetSkill(DecalFilters::eAttributeCoordination)

How can I do this in a compiled framework? In a script environment I could simply make a 3rd constant and do (in essence) this: a = ("DecalFilters::eAttribute" + a). This one has me baffled in how to do it via C++ and yes a and b could be anything in their strings.
 
Not entirely sure I understand the question, a and b are strings, how do they relate to the GetSkill call you make?

I get the feeling the answer is something about OO and method overloading...

/Per

"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."
 
Suppose we have enum eType in DecalFilters class...
See (working;) sceleton (short names for brevity;):
Code:
#include <iostream>
#include <string>
using namespace std;

class Filters
{
public:
  enum eType { e1st, e2nd, e3rd, eBad }; // Add 'bad' value
  void doThat(eType what) { cout << &quot;Do &quot; << what << endl; }
};
// Include this function as Filters member if possible...
Filters::eType eByName(string ename)
{
  Filters::eType ewhat = Filters::eBad;

  if (ename == &quot;e1st&quot;)
    ewhat = Filters::e1st;
  else if (ename == &quot;e2nd&quot;)
    ewhat = Filters::e2nd;
  else if (ename == &quot;e3rd&quot;)
    ewhat = Filters::e3rd;
// Append new ops if you need...
  return ewhat;
}

int main(int argc, char* argv[])
{
  Filters f;
  Filters::eType what;

  string cmd;
  while (cout << &quot;Enter op:&quot;,cin >> cmd)
  {
    if ((what=eByName(cmd)) != Filters::eBad)
      f.doThat(what);
    else
      cout << &quot;No op, try again..,&quot; << endl;
  }
  return 0;
}
There are 100000 variances, but in a compiled framework you must write some kind of switching code. Pay for speed and safety...
Let's suppose once morning your interpreter gets a == &quot;NoFocusOrCoordinationGoHome&quot;. Just imagine!..
 
This is what I came up with but I was so hoping for something more eloquent.

if (First == &quot;Strength&quot;)
a= m_pCharStats->GetEffectiveAttribute(DecalFilters::eAttrStrength);
else if (First == &quot;Endurance&quot;)
a= m_pCharStats->GetEffectiveAttribute(DecalFilters::eAttrEndurance);
else if (First == &quot;Quickness&quot;)
a= m_pCharStats->GetEffectiveAttribute(DecalFilters::eAttrQuickness);
else if (First == &quot;Focus&quot;)
a= m_pCharStats->GetEffectiveAttribute(DecalFilters::eAttrFocus);
else if (First == &quot;Self&quot;)
a= m_pCharStats->GetEffectiveAttribute(DecalFilters::eAttrSelf);
else
a = 1;

This is untested code until later today but this is what I did right after I wrote this and figured I would take the path of least resistance.

Is this the really the best way?

Thanks for the replies so far. :)
 
Another way to do it:

Code:
#include <string>
#include <map>

class Filter
{
	public:
		enum eType { eAttrStrength, eAttrEndurance, etc ...}; 

		Filter() 
		{ 
			mMap[&quot;Strength&quot;] = eAttrStrength;
			mMap[&quot;Endurance&quot;] = eAttrEndurance;
 			etc
              .
              .
		}

		eType getEnumByName(const std::string& ename) const
        {
          // Look up the ename in the map
		  MapStringToEnum::const_iterator i = mMap.find(ename);

          // Didnt find it?
		  if (i == mMap.end())
			throw &quot;Unsupported enum name&quot;; // Or use a nice exception class

		  // Found it - return the enum
		  return (*i).second;
		}
        

    private:
		typedef std::map<std::string, eType > MapStringToEnum;
        MapStringToEnum mMap;
};

1) Using a std::map to resolve the if-else-if-else if... chunk.
2) Not allowing a &quot;bad&quot; enum, don't even define one. Ie you can rely on that eType is always a valid value.
3) Adding more attrs is easy. Just put a new enum in the enum def and a new assignment in the constructor. getEnumByName won't be affected...


/Per

&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;
 
Now thats pretty sweet and I have as yet to touch maps (heck when I did C I do not even think I did them then).

Very nice indeed and now to dust off the books on the map feature. :)

Thank you.
 
>Now thats pretty sweet and I have as yet to touch maps

As I've already had my wow-that-map-thing-is-soo-cool experience and it felt good, I envy you :)

>heck when I did C I do not even think I did them then

std::map came with c++.





/Per

&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top