Archived

This topic is now archived and is closed to further replies.

MrPlough

Enumerating 3d devices in a class.

Recommended Posts

Hi, Im trying to get the Enumerate 3d devices routine in the DX7 SDK to work from inside a class. Im getting the following errors on the callback function: if ((lpd3dEnumDeviceDesc->dwDeviceRenderBitDepth & VPbitdepth) == 0) { return D3DENUMRET_OK; } error C2597: illegal reference to data member ''Cd3d::VPbitdepth'' in a static member function. CopyMemory(&g_guidDevice, &lpd3dEnumDeviceDesc->deviceGUID, sizeof(GUID)); error C2664: ''memcpy'' : cannot convert parameter 1 from ''struct _GUID Cd3d::*'' to ''void *''. and a few more along the same lines... The routine worked fine until i put it in a class. Its probably because im misusing the class in some way. help please Mr Plough

Share this post


Link to post
Share on other sites
This is really just a C++ issue with static functions.

A class of is really just a description of how to make an object or ***instance*** of that type. For example say I had a class called MyClass, I could create two instances of that somewhere:

MyClass myObject_instance1;
MyClass myObject_instance2;


A static member function is really just like a global function except it has a name based on the class name. A static member function ***cannot*** access member variables of an object since a static function by nature doesn''t have a this pointer and so has no knowledge of which of the instances the variable lives in.

Solutions?

1) You could make the variables accessed inside the static function static themselves so that they are shared across all instances of the class.

2) On DirectX callback functions you''ll see a void pointer called "context" or similar. You can use this to pass instance information (i.e. a "this" pointer) to the callback:


// the caller - passes the "this" pointer as the context
// also sets a member variable for this instance to something
MyClass::StartEnumeratingDevices()
{
m_someMemberVariable = 9182;
pSomeDirectXInterface->EnumerateSomething(..., this );
}

// the static callback - fetches the context as a local instance
// pointer and accesses the member variable through that
MyClass::EnumCallback(..., void* context )
{
MyClass* pInstance = (MyClass*)context;

pInstance->m_someMemberVariable = 7162;
}


Access any per-instance member variables in the static callback via pInstance.

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

Share this post


Link to post
Share on other sites