HOW TO??? call an unknown function's method

P

paolo.dx

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
 
A

Alf P. Steinbach

* (e-mail address removed):
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()

The above shouldn't compile then (also macros are generally Not A Good
Idea, I think you'll find the FAQ referring to them as Evil).

Post something complete that compiles except for your problematic call.

Mark that call with a comment, and try to describe where you get the
information about what to call.
 
V

Victor Bazarov

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){

What's that? A macro? What does it expand into? Ah, I see... A mere

struct myclass {

So, why do you think you need that crap? Just write "struct myclass".
int counter;

void myfunc();

CTOR(conta){

Huh? This should not compile. Your 'CTOR' will expand into

conta(){

which is definitely not the same as the name of the class ("myclass") you
just gave it above...
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();

What's "myclass"? Is that the same as the name of the struct you just
defined? Then you cannot use the "dot" notation. You either need
a temporary of the type 'myclass' or you need the scope resolution:

myclass().myfunc();

or

myclass::myfunc();

depending on what you really need it for.
WAIT();
}
}

the function method can take everthing you want.

It all sounds (and looks) like a REALLY BAD IDEA(tm). Why do you think
you need all that macro nonsense? What is your ultimate goal here? It
does seem that templates are a better choice.

V
 
M

mlimber

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top