pa******@gmail.com wrote:
Hello, I have to call a member funtion "myclass.myfunc()" from another
function "void method();" but I don't know the name of "myclass" and
"myfunc()" because I'm writing a library.h that has to be put in the
header of a given file.
this is an example of the given file, obviously I can't modify this
file, but I know that the funcitions are always without arguments and
void type.
CLASS(myclass){
int counter;
void myfunc();
CTOR(conta){
TYPE_METHOD(myfunc);
conteggio=0;
};
};
I redefine CLASS and CTOR with these define
#define CLASS(nome_classe) \
struct nome_classe
#define CTOR(nome_func) \
nome_func()
and I can redefine TYPE_METHOD using #define with whatever I want, but
there must be the call to "method(....);
void method(.....){
while(1){
myclass.myfunc();
WAIT();
}
}
the function method can take everthing you want.
Thanks for any help!
Paolo
As Alf and Victor noted, it's unclear exactly what you're trying to do.
Depending on your design, you might be able to accomplish what you want
with standard inheritance and virtual functions (probably the preferred
method):
struct A
{
virtual void myfunc() { ... }
};
struct B : A
{
virtual void myfunc() { ... }
};
struct C
{
void foo( A& a ) { a.myfunc(); }
}
Or, you might need to use some combination of templates and functors.
If myfunc is always the name of the function, you could create a class
that calls it on an arbitrary type like this:
struct C
{
template <class T>
void foo( T& t )
{
// ...
t.myfunc();
}
};
Or you could use the standard library's (or Boost's or your own)
functionals to create a functor object:
struct C
{
template <class Fn>
void foo( Fn fn )
{
// ...
fn();
}
};
where the functor fn invokes your function on your particular object
with any necessary parameters, akin to std::mem_fun_ref and
std::bind1st.
Cheers! --M