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



 DEVELOP > c-Plus-Plus > HOW TO??? call an unknown function's method

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 17 Mar 2006 07:56:17 AM
Object: HOW TO??? call an unknown function's method
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
.

User: "Alf P. Steinbach"

Title: Re: HOW TO??? call an unknown function's method 17 Mar 2006 08:04:02 AM
* paolo.dx@gmail.com:

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.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.

User: "mlimber"

Title: Re: HOW TO??? call an unknown function's method 17 Mar 2006 08:42:27 AM
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
.

User: "Victor Bazarov"

Title: Re: HOW TO??? call an unknown function's method 17 Mar 2006 08:12:51 AM
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){

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
--
Please remove capital As from my address when replying by mail
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER