Cast a member function pointer
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 13/09/2004 at 00:48, xxxxxxxx wrote:
User Information:
Cinema 4D Version: 8.207
Platform: Windows ;
Language(s) : C++ ;---------
The origin of my problem is a bit more c++ than sdk, but I ask it here, because it can help alot when creating class libraries.
For a class library I have to create an interface, using a C4DLibrary derived struct to save the member function pointers, and define the internal class. I want to write kind of module handler, which defines the interface of my modules, takes care for the registration of modules and so on. The modules itself should be the internal part of a class library, derived from a class defined in the module handler.
This leads to a problem: the module handler defines the interface, like
class iImportModule {
public:
void test();
static iImportModule* Alloc( );
static void Free( iImportModule*& p );
};
class ImportModule {
public:
void test();
static ImportModule* Alloc();
static void Free( ImportModule*& p );
};struct LIB_ImportModuleLib : public C4DLibrary {
void ( iImportModule::*test )( );
iImportModule* ( *Alloc )( );
void ( *Free )( iImportModule*& p );
};
And in my modules I want to derive the module class:
class testmodule : public iImportModule
So the modules have the functions called by the module handler, and can have internal functions/variables to do theire job.
But when registering I have to fill out the library struct. The problem is, that I want to define the library struct in the module handler, this interface should not change. But there I only know the parent class of the modules, but not the module class itself:
In the module I have to do:
LIB_ImportModuleLib vlib;
vlib.test = &testmodule::test;
which will run into a problem: the cast cannot be done. Anyone has a idea, how to do the cast ( basically it should just be a 32bit adress? ), or another way how to solve it?
I could do my modules as function libs, did that for another task, works fine, but here I want to have class libs. Maybe also possible would be to redefine the library struct in the module, be sure that its memory compatible with the one from the module, and register this one...but thats the same interface defined at two positions, I want to avoid this. -
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 17/09/2004 at 07:43, xxxxxxxx wrote:
Why does the assignment
vlib.test = &testmodule;::test;
not work? What is the exact compiler error? As far as I can see, this should work, since &testmodule;::test has the correct type to be assigned to vlib.test. No cast should be needed. gcc compiles the code you provided without any complaints, so maybe it is compiler-specific.
Michael
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 20/09/2004 at 06:05, xxxxxxxx wrote:
Found the solution.....
virtual functions in the base class, and vlib.test = &iImportModule::test. The V-Table of the base class is changed be the derived testmodule, so &iImportModule::test actually points to the function in testmodule.