Skip to main content
gamedev.net
🔒 Locked

Newbie Question

Started by Antion Dec 23, 2001 at 1:17 AM 0 replies 500+ views
Original Post
Antion
Antion
Ok, which graphic API is better Direct X or Open Gl (just kidding) Actually my question is this. I am currently learning C++ (from like 3 different books) and the one question that I am coming up with is with Conversion Constructors and Member Conversion Functions. What exactly are they used for. Ok, the books say to make an object of one type to another but what would you use that for? I know this is probably a very naive question, but it is something that I haven't found in any of my books. One example in my book converts a Xmas object to a DayOfYear object, this example really doesn't get across the idea. What am I missing? Thanks ~Antion Edited by - antion on December 23, 2001 2:17:42 AM Edited by - antion on December 23, 2001 2:30:25 AM
Shannon Barber
Shannon Barber
It''s most useful for utility classes, classes that wrap-up something you use or do alot.

This is a simple class I made to make dealing with events trivial. But in order to use my class in-place of a Windows Event handle, it needs to be able to convert itself into a HANDLE. That''s what operator HANDLE() does.
  
class CEvent
{
public:
enum eResetMode {AutoReset=FALSE, ManualReset=TRUE};
CEvent(eResetMode ResetMode=AutoReset, BOOL bInitialState=FALSE, LPSECURITY_ATTRIBUTES pSecAtr=NULL, LPCTSTR szName=NULL) : m_hEvent(0)
{
m_hEvent = CreateEvent(pSecAtr, bManualReset, bInitialState, szName);
assert(m_hEvent);
}

CEvent(HANDLE hEvent) : m_hEvent(hEvent)
{
}

CEvent(const CEvent& cEvent)
{
m_hEvent = cEvent.m_hEvent;
}

~CEvent()
{
if(m_hEvent)
CloseHandle(m_hEvent);
}

operator HANDLE()
{
assert(m_hEvent);
return(m_hEvent);
}

HANDLE* operator&()
{
assertMsg(m_hEvent==NULL, "This usually indicates a bug");
return &m_hEvent;
}

BOOL Signal()
{
assert(m_hEvent);
return SetEvent(m_hEvent);
}

BOOL Reset()
{
assert(m_hEvent);
return ResetEvent(m_hEvent);
}

BOOL Pulse()
{
assert(m_hEvent);
return PulseEvent(m_hEvent);
}
public:
HANDLE m_hEvent;
};
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.