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!

enum help needed.

Status
Not open for further replies.

DarkAlchemist

Programmer
Jan 19, 2004
18
US
I have a structure and a function that uses enum XXXX *pVal this and I have never enum like that before. Does anyone have any ideas what parameter I need to create to call the function?

Here is what they look like:
Code:
STDMETHOD(get_Training)(/*[out, retval]*/ enum eTrainingType *pVal)

STDMETHODIMP cSkillInfo::get_Training(enum eTrainingType *pVal)
{
   if( pVal == NULL )
   {
      _ASSERT( FALSE );
      return E_POINTER;
   }

   *pVal = m_pSkill->m_trained;

        return S_OK;
}

AND

STDMETHODIMP cACHooks::get_SkillTrainLevel( eSkill SkillID, eTrainLevel *pVal )   
{   
	struct qSkill Skill;   
	HRESULT hr;   
	hr = GetSkillInfo(SkillID, &Skill);   
	if (FAILED(hr)) {   
		return hr;   
	}   

	*pVal = Skill.Trained;   

	return S_OK;   
}

Thank you for any guidance you offer. :)
 
Well, its just a pointer. And a pointer is just an address. So you're supposed to pass it an address to an eTrainingType

eTrainingType e;
theSkillInfo.get_Training(&e);

/Per

"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."
 
Code:
STDMETHOD(get_Training)(/*[out, retval]*/ enum eTrainingType *pVal)

STDMETHODIMP cSkillInfo::get_Training(enum eTrainingType *pVal)

Function "get_Training" accepts a parameter that is acting also as a return value. From the code, I see you have to pass a vaild pointer to this function.
To call it, I might do :
Code:
  enum eTrainingType training_type;
  if (skill_info.get_Training (&training_type) == S_OK)
  {
    //training_type is initialized now
    //...
  }

I would do the same thing for the second function.

--
Globos
 
Is there any reason to use enum in this way? Seems rather weird to me when a simple long or int would do the same (considering the enum has already taken place at compile time). The returned value is one of 4 states and the 4 states are 0, 1, 2, 3, 2^31 <- That state is just weird but thats how its defined.
 
error I meant

typedef enum {
eUntrained = 1,
eTrained = 2,
eSpecialized = 3,
eTrainLevel_DWORD = 2147483647
} eTrainLevel;

just to clarify. :)
 
>Is there any reason to use enum in this way?

Well, as I'm generally opposed to passing pointers I would say no :)

The point of enums is that they provide typechecking, ie you can't pass the function an int (not without evil casting anyway).

Ie later when you deal with the enum you dont have to worry about all possible invalid int values, but can just focus on the ones defined for the enum.

int = enum is always possible
enum = int fails thanks to typechecking.

/Per

&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;
 
As you mention:
&quot;The returned value is one of 4 states &quot;

Indeed, and the enum helps you keep it that way, since you'd have to explicitly cast to make it hold anything else.

Compare that to an int, which can hold one of 4294967295 possible states.

Ie with the enum you can easily ignore the 4294967291 other states that doesn't apply :)

/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