Enumerating 3d devices in a class.

Started by
0 comments, last by MrPlough 21 years, 3 months ago
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
Advertisement
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 somethingMyClass::StartEnumeratingDevices(){  m_someMemberVariable = 9182;  pSomeDirectXInterface->EnumerateSomething(..., this );}// the static callback - fetches the context as a local instance// pointer and accesses the member variable through thatMyClass::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

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

This topic is closed to new replies.

Advertisement