Edit How is this in any way related to the supposed post? They are both completely different errors, you guys should really stop trying to farm rep
So I've been searching around google and stackoverflow but I couldn't find one solution that would help my case.
I have a D3D9Device pointer and I want the EndScene address of that device, how would I approach so?
DWORD aEndScene = *(DWORD*)(&d3ddev->EndScene);
won't work with the following error
'&': illegal operation on bound member function expression
I think that's wrong because I'm actually trying to get the address of the d3ddev class
Member function pointers are not per object, they are per type. In your example, you could have multiple instances of a IDirect3DDevice9, all of which would have the same pointer value for their EndScene member function (assuming they aren't different concrete types - but this isn't likely).
The specific error you are getting is because you are attempting to get the address of a pointer-to-member function from an object, which isn't valid (eg. see '&' illegal operation on bound member function expression error).
It is possible to get the value of a member function using the type, instead of an object pointer. However, it's extremely ugly:
// Value is stored in 'end_scene':
HRESULT (IDirect3DDevice9::* end_scene)() = &IDirect3DDevice9::EndScene;
// Call the function, with the value of 'end_scene'.
(*d3ddev.*(end_scene))();
// Print the address of the pointer-to-member function:
printf("%p\n", end_scene);
I wouldn't suggest doing this here, because most functions in IDirect3DDevice9 don't have the same prototype. In fact, only BeginScene has the same prototype as EndScene, and it's hard to imagine a situation in which the call could be one or the other, since they need to be called in a specific order. You could make the case about using this for the functions that get/set vertex/pixel shader constants, as they have the same prototypes, but, it's just as easy to store some other external state to determine which function to call, and much more straightforward.
Related
I'm currently learning about virtual functions, and in this particular lesson it creates an array of object pointers firstArray[5], and calls functions from these objects. Until now, whenever I wanted to call a function func() from an object foo, I would write foo.func(). Now that I'm using virtual functions and this array, the book has switched to this method: firstArray[0]->func(). The book doesn't do a great job at justifying this switch, could someone please explain? I see that when I try to use firstArray[0].func(), I get this....
error: request for member 'func' in 'firstArray[0]', which is of non-class type 'sampleClass*'.
Is it simply because I'm trying to call a function from a pointer, not an actual object? I've been learning C++ for several months now, and for whatever reason pointers still trip me up sometimes. Any clarification would help.
EDIT:
I think the part that got me mixed up is this: I can create a pointer to a base object class with base *ptr;. Then, I can set that pointer by creating a new object from a derived class by saying ptr = new derived;. This is where I get confused. If I were to create an int* ptr;, and I wanted it to point to a integer I create, I couldn't say ptr = int j. If ptr is really just an address, why do these two examples work differently? I guess I don't understand the "new" mechanic very well either.
That doesn't have anything to do with virtual functions. If you have a pointer you need operator-> to deference and access the object it's pointing to....
You could still use the operator. (dot) to access the members/functions if you dereference first:
(*foo).func()
A pointer to an object just holds the address where the object is held in memory.
So if you have a variable which is a pointer to some type, it actually holds a number.
If you want to use the object, you need to call the object it is pointing to, which is as I said by using either operator* or operator. (dot).
I'm currently trying to design a property system, to bind member variables of a few classes to a serializer, and I want to write the least possible code for each binding, and yet be flexible.
I think getters/setters aren't really necessary most of the time, so they would only be used when they actually trigger something. The classes would provide a list of variable names, and either a pointer to the variable, either a pointer to getters/setters.
My questions are :
Is binding by pointer actually dangerous or even moral?
Can these classes give these pointers without knowing their actual instance? (ie get binding info once for all instances of each class, and store that somewhere). AFAIK, Boost::bind doesn't allow that.
You should consider using boost::property_map
http://www.boost.org/doc/libs/1_49_0/libs/property_map/doc/property_map.html
Dangerous yes, immoral no. You can make the classes be friends of the serializer and hide the binding stuff for mortals to improve safety, then you have a set of related classes which are morally allowed to know about each others internal structure.
The class can definitely return the binding info, for instance as byte offsets. It may be easiest though if the class owns a "prototype" object of that class (i.e. static member of its own type). Then by getting the address of a prototype field as a const char * and subtracting from the address of the prototype also as a const char * you get the byte offset for the field.
Of course, then you need to make sure you know what type the field is, so you can correctly manipulate the data given a byte offset (e.g. cast back to the correct pointer type).
However there are many gotchas around implementing something like this, which mostly revolve around making sure you have the correct pointer type when serializing, rather than a pointer to some subobject within the object.
I have one clarification
What is the difference between calling a function through function pointer and calling a function directly by name ?
Anybody help me in this.
There is no difference in the actual call. Parameters are passed the same way, the function runs the same way, and the return value comes back the same way.
The only difference is that you can make the function pointer point somewhere else.
There is no difference except that a compiler/linker calculates exactly what address to transfer control of the program to when you call a function by name and hardcodes that value into the code, whereas with function pointers, the computer must use the pointer to calculate where to transfer control to at runtime.
No difference (except that calling by name will always call the same function, and pointer can be changed to point to different functions).
While the direct use of function pointers does not have any cost, you should bear in mind that function pointers aren't compile time constants, so it maybe has a cost to read them. So if you have a function pointer inside a class and use that to emulate polymorphic behavior, you won't get any speedup at all.
I've got a function that takes a list of CRuntimeClass pointers in order to setup a view. I'd like to return without doing anything if the function is called with a list of the same classes that are already setup. Saving the pointer values and comparing them on the next call is currently working, but I want to verify that that's a legal thing to do, and not something that just happens to work. Maybe my doc-search-fu is lacking, but I can't find anywhere that guarantees the pointer value returned from the RUNTIME_CLASS() macro for a given class will be the same for the life of the program. The closest I could find is in the docs for CObject::GetRuntimeClass():
There is one CRuntimeClass structure for each CObject-derived class.
That implies that the pointer value shouldn't change, but doesn't exactly state it. Does anyone have something a bit more concrete on that? Or is there a better way to compare the CRuntimeClasses?
No such guarantee is documented, albeit that it is likely. You are supposed to use CObject::IsKindOf().
Taking a peek at afx.h plus a little of debugging shows that RUNTIME_CLASS() returns a pointer to a static member: static CRuntimeClass class##class_name (as it can be seen in the definition of DECLARE_DYNAMIC(class_name) macro).
As the member is static, the pointer to it does not change during runtime. In other words static is your guarantee.
So, I'm using the FMOD api and it really is a C api.
Not that that's bad or anything. Its just it doesn't interface well with C++ code.
For example, using
FMOD_Channel_SetCallback( channel, callbackFunc ) ;
It wants a C-style function for callbackFunc, but I want to pass it a member function of a class.
I ended up using the Win32 trick for this, making the member function static. It then works as a callback into FMOD.
Now I have to hack apart my code to make some of the members static, just to account for FMOD's C-ness.
I wonder if its possible in FMOD or if there's a work around to link up the callback to a specific C++ object's instance member function (not a static function). It would be much smoother.
You cannot directly pass a member function. A member function has the implicit parameter this and C functions don't.
You'll need to create a trampoline (not sure the signature of the callback, so just doing something random here).
extern "C" int fmod_callback( ... args ...)
{
return object->member();
}
One issue is where does that object pointer come from. Hopefully, fmod gives you a generic context value that will be provided to you when your callback is made (you can then pass in the object pointer).
If not, you'll just need to make it a global to access it.
I guess it supposed to work like this:
You can assign some user data to channel by calling FMOD_Channel_SetUserData. This user data should be a pointer to your C++ object that handles events.
Then you should write C-style callback that extracts that object by calling FMOD_Channel_GetUserData and then calls your C++ instance method on that object.
There is a non-portable, and pretty hackish solution that has the advantage of at least being thread-safe, which the "trampoline" methods are not.
You can generate the actual function machine code on the fly. The basic idea is that you have a template for your call-back function that takes an object pointer and a member-function pointer and gives you a block of heap memory that you can pass to the library as a C call-back function, that will, when called, turn around and call the member function on that object.
It's messy, and you'll have to provide an implementation for any new platform (any time the calling convention changes), but it works, is thread-safe. (Of course you'll also have to watch out for DEP). The other thread-safe solution is to resort to thread-local storage (assuming that you know the call-back will happen on the same thread as the call you made).
See http://www.codeproject.com/KB/cpp/GenericThunks.aspx for an example of how you could go about generating thunks.
Using only a function pointer (and no additional separate object pointer) for a C callback is a broken design, in my humble opinion.
If the function were, instead, FMOD_Channel_SetCallback(channel, callbackFunc, callbackObj), then your static method just takes an instance of the object, then calls callbackObj->func() (which obviously can be non-static).
you need to use a trampoline and store the pointer to the object you want to get the member function called on in a global or static variable, i.e.
Object *x;
void callback_trampoline() { x->foobar(); }
...
FMOD_Channel_SetCallback(CHANNEL, callback_trampoline);