Sign in to follow this  

ZeroMemory in a constructor

This topic is 3628 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
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

This topic is 3628 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this