Jump to content
  • Advertisement
Sign in to follow this  
justcallmedrago

ZeroMemory in a constructor

This topic is 3967 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just can't figure out how to do this. Microsoft's XInput sample:
DWORD dwResult;    
for (DWORD i=0; i< MAX_CONTROLLERS; i++ )
{
	XINPUT_STATE state;
	ZeroMemory( &state, sizeof(XINPUT_STATE) ); //Important part here

        // Simply get the state of the controller from XInput.
        dwResult = XInputGetState( i, &state );

        if( dwResult == ERROR_SUCCESS )
	{ 
	    // Controller is connected 
	}
        else
	{
            // Controller is not connected 
	}
}
My struct:
struct CONTROLLER{
	DWORD connected;
	DWORD controllerNumber;
	XINPUT_STATE state;
	CONTROLLER();
};

CONTROLLER::CONTROLLER()
:
 ZeroMemory( &state, sizeof(XINPUT_STATE) ); //Doesn't work
{
}
How do I use ZeroMemory in a constructor? I want to fill the state member with zeros right when a CONTROLLER struct is created. Can I do this? Is this even necessary? Also, what do structs look like before data is put in them? (why is ZeroMemory useful?)

Share this post


Link to post
Share on other sites
Advertisement
CONTROLLER::CONTROLLER() 
{
ZeroMemory( &state, sizeof(XINPUT_STATE) );
}

//or

CONTROLLER::CONTROLLER()
{
ZeroMemory( this, sizeof(CONTROLLER) ); // clear whole struct to 0
}

//or

template&lt; class T &gt;
struct EmptyStruct : public T
{
EmptyStruct()
{ ZeroMemory( this, sizeof(T) );
};

EmptyStruct&lt;CONTROLLER&gt; controller;






Share this post


Link to post
Share on other sites
You can't put executable code in the initialisation list. Unless XINPUT_STATE provides a constructor that does this then you must make the call from the body of your constructor:

struct CONTROLLER {
DWORD connected;
DWORD controllerNumber;
XINPUT_STATE state;
CONTROLLER();
};

CONTROLLER::CONTROLLER() {
ZeroMemory( &state, sizeof(XINPUT_STATE) );
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!