By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,192 Members | 858 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,192 IT Pros & Developers. It's quick & easy.

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

P: n/a
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[i] == (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[i] = argv[i];

}
}

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.
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Thu, 20 Nov 2003 12:30:26 -0800, Christian Mörck wrote:
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.
Jul 22 '05 #2

P: n/a
Christian Mörck wrote:
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[i] == (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[i] = argv[i];

}
}

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[i] == (string)ArgOpt) {
(this->*ArgOptFunc)();
}
return true; // you've define ArgFunc as returning a bool, so return something!!!!!
}

Jul 22 '05 #3

P: n/a
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.
Jul 22 '05 #4

P: n/a
red floyd <no*****@here.dude> wrote in message news:<yG***************@newssvr14.news.prodigy.com >...
Christian Mörck wrote:
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[i] == (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[i] = argv[i];

}
}

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[i] == (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.
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.