Problem posting a method as an argument to an method..

  • Thread starter =?ISO-8859-1?Q?Christian_M=F6rck?=
  • Start date
?

=?ISO-8859-1?Q?Christian_M=F6rck?=

First of all, i would like to ask if this is even possible? Can you
post a method as an argument to an method?


Excuse the 'newbie' code.

======== Arghandler.h =============

class arghandler {

int count;
string *p_Argarray;
char m_error_msg[25];

public:

//Constructor
arghandler(int argc, char *argv[]);

//Destructor
~arghandler();

//Argument functions
bool ArgFunc(char *ArgOpt, void (*ArgOptFunc)());

//Help function
void help();


};

#include "arghandler.cpp"


======== Arghandler.cpp ==========
bool arghandler::ArgFunc(char *ArgOpt, void (*ArgOptFunc)()) {

for(int i=0;i<count;i++) {

if(p_Argarray == (string)ArgOpt) {

(*ArgOptFunc)();

}
}

return true;

}


void arghandler::help() {

cout << "HELP!\n";
exit(1);
}

arghandler::arghandler(int argc, char *argv[]) {

p_Argarray = new string[argc];
count = argc;

for(int i=0;i<argc;i++) {

p_Argarray = argv;

}


}

arghandler::~arghandler() {

delete [] p_Argarray;

}


=============== main.cpp ===========

#include "arghandler.h"

int main(int argc, char *argv[]) {

arghandler handler(argc, argv);

//I know this works if i send an function, but how
//does it work with sending an method - if it even do.
handler.ArgFunc("--help", help);

return 0;

}

=================================

handler.ArgFunc("--help", help); <--- this works if i define help() an
an usual function, but thats not what i want. I want it to take an
method as an argument.

Once again i would like to apologize for my code and if my question is
'newbie' or somewhat else.
 
N

Nathanael D. Noblet

First of all, i would like to ask if this is even possible? Can you
post a method as an argument to an method?

First off as a disclaimer, I haven't looked over your code thouroughly.
The simple answer is yes. There are complexities involved though for
example you can let it pass a pointer to a function, but you'd have
problems passing that methods arguments(if any) without first declaring
them in the method you want to pass them to or using the printf{...}
tricks. Make sense? As well you'd have problems restricting what type
of functions could be called, creating possible undefined behavior,
if you call with false or unexpected functions, who knows what it'll do.
 
R

red floyd

Christian said:
First of all, i would like to ask if this is even possible? Can you
post a method as an argument to an method?


Excuse the 'newbie' code.

======== Arghandler.h =============

class arghandler {

int count;
string *p_Argarray;
char m_error_msg[25];

public:

//Constructor
arghandler(int argc, char *argv[]);

//Destructor
~arghandler();

//Argument functions
bool ArgFunc(char *ArgOpt, void (*ArgOptFunc)());

//Help function
void help();


};

#include "arghandler.cpp"


======== Arghandler.cpp ==========
bool arghandler::ArgFunc(char *ArgOpt, void (*ArgOptFunc)()) {

for(int i=0;i<count;i++) {

if(p_Argarray == (string)ArgOpt) {

(*ArgOptFunc)();

}
}

return true;

}


void arghandler::help() {

cout << "HELP!\n";
exit(1);
}

arghandler::arghandler(int argc, char *argv[]) {

p_Argarray = new string[argc];
count = argc;

for(int i=0;i<argc;i++) {

p_Argarray = argv;

}


}

arghandler::~arghandler() {

delete [] p_Argarray;

}


=============== main.cpp ===========

#include "arghandler.h"

int main(int argc, char *argv[]) {

arghandler handler(argc, argv);

//I know this works if i send an function, but how
//does it work with sending an method - if it even do.
handler.ArgFunc("--help", help);

return 0;

}

=================================

handler.ArgFunc("--help", help); <--- this works if i define help() an
an usual function, but thats not what i want. I want it to take an
method as an argument.

Once again i would like to apologize for my code and if my question is
'newbie' or somewhat else.


Your problem is that help() is a member function. It requires a "this" pointer.
Redefine your argfunc this way:

bool arghandler::ArgFunc(char *ArgOpt, void (arghandler::*ArgOptFunc)())
{
for(int i=0;i<count;i++)
{
if(p_Argarray == (string)ArgOpt) {
(this->*ArgOptFunc)();
}
return true; // you've define ArgFunc as returning a bool, so return something!!!!!
}
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

Christian Mörck escribió:
=============== main.cpp ===========

#include "arghandler.h"

int main(int argc, char *argv[]) {

arghandler handler(argc, argv);

//I know this works if i send an function, but how
//does it work with sending an method - if it even do.
handler.ArgFunc("--help", help);

return 0;

}

=================================

handler.ArgFunc("--help", help); <--- this works if i define help() an
an usual function, but thats not what i want. I want it to take an
method as an argument.

If help were a static member you can do: handler.ArgFunc ("--help", &
arghandler::help);

Being a non static member you can't. Pointers to member funtions and
pointer to ordinary functions are not compatible. You need another
ArgFunc version that takes a pointer to member function argument.

Regards.
 
C

Christian M?rck

red floyd said:
Christian said:
First of all, i would like to ask if this is even possible? Can you
post a method as an argument to an method?


Excuse the 'newbie' code.

======== Arghandler.h =============

class arghandler {

int count;
string *p_Argarray;
char m_error_msg[25];

public:

//Constructor
arghandler(int argc, char *argv[]);

//Destructor
~arghandler();

//Argument functions
bool ArgFunc(char *ArgOpt, void (*ArgOptFunc)());

//Help function
void help();


};

#include "arghandler.cpp"


======== Arghandler.cpp ==========
bool arghandler::ArgFunc(char *ArgOpt, void (*ArgOptFunc)()) {

for(int i=0;i<count;i++) {

if(p_Argarray == (string)ArgOpt) {

(*ArgOptFunc)();

}
}

return true;

}


void arghandler::help() {

cout << "HELP!\n";
exit(1);
}

arghandler::arghandler(int argc, char *argv[]) {

p_Argarray = new string[argc];
count = argc;

for(int i=0;i<argc;i++) {

p_Argarray = argv;

}


}

arghandler::~arghandler() {

delete [] p_Argarray;

}


=============== main.cpp ===========

#include "arghandler.h"

int main(int argc, char *argv[]) {

arghandler handler(argc, argv);

//I know this works if i send an function, but how
//does it work with sending an method - if it even do.
handler.ArgFunc("--help", help);

return 0;

}

=================================

handler.ArgFunc("--help", help); <--- this works if i define help() an
an usual function, but thats not what i want. I want it to take an
method as an argument.

Once again i would like to apologize for my code and if my question is
'newbie' or somewhat else.


Your problem is that help() is a member function. It requires a "this" pointer.
Redefine your argfunc this way:

bool arghandler::ArgFunc(char *ArgOpt, void (arghandler::*ArgOptFunc)())
{
for(int i=0;i<count;i++)
{
if(p_Argarray == (string)ArgOpt) {
(this->*ArgOptFunc)();
}
return true; // you've define ArgFunc as returning a bool, so return something!!!!!
}



Thank you so much....

About the return; The function isnt completely done yet, i wanted this
to work first. Thanks again.
 

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
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top