E
Eric Lilja
Using a macro, can I change what type an object is being cast to?
I know, the initial respone to question might be an instinctive "ugly, don't
even think about it!" or "don't use macros at all", but I still would like
to know because I am trying to encapsulate a third-party C-based
callback-oriented gui toolkit inside C++ classes and I would like to try
something.
So say you have:
long some_address = some_valid_address;
((SomeClass*)some_address)->foo();
I want to test something and I would like to know if I can write a macro
that casts some_address to the pointer of another class (actually a subclass
of SomeClass). Since macros are part of the preprocessor and the
preprocessor is run before the compiler, a macro can replace the actual code
seen by the compiler, yes? I don't think I've ever made my own macro, I only
use the preprocessor for #ifndef, #ifdef etc.
So why do I want to do this? Well, as I said I am trying to encapsulate a
third-party C-based gui toolkit that revolves around callbacks. My parent
class has a static member function that acts a dummy callback (called by the
system), and using the parameters it finds a pointer to itself (actually a
pointer to a subclass) and uses that to call non-static member functions.
The problem is that the system seems to be calling this static member
function before the subclass object has been constructed fully thus calling
the parent version of some virtual functions the first few calls.
Thanks for reading and replying
/ Eric
I know, the initial respone to question might be an instinctive "ugly, don't
even think about it!" or "don't use macros at all", but I still would like
to know because I am trying to encapsulate a third-party C-based
callback-oriented gui toolkit inside C++ classes and I would like to try
something.
So say you have:
long some_address = some_valid_address;
((SomeClass*)some_address)->foo();
I want to test something and I would like to know if I can write a macro
that casts some_address to the pointer of another class (actually a subclass
of SomeClass). Since macros are part of the preprocessor and the
preprocessor is run before the compiler, a macro can replace the actual code
seen by the compiler, yes? I don't think I've ever made my own macro, I only
use the preprocessor for #ifndef, #ifdef etc.
So why do I want to do this? Well, as I said I am trying to encapsulate a
third-party C-based gui toolkit that revolves around callbacks. My parent
class has a static member function that acts a dummy callback (called by the
system), and using the parameters it finds a pointer to itself (actually a
pointer to a subclass) and uses that to call non-static member functions.
The problem is that the system seems to be calling this static member
function before the subclass object has been constructed fully thus calling
the parent version of some virtual functions the first few calls.
Thanks for reading and replying
/ Eric