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

Discussion in 'C++' started by =?ISO-8859-1?Q?Christian_M=F6rck?=, Nov 20, 2003.

  1. 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.
    =?ISO-8859-1?Q?Christian_M=F6rck?=, Nov 20, 2003
    #1
    1. Advertising

  2. 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.
    Nathanael D. Noblet, Nov 20, 2003
    #2
    1. Advertising

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

    red floyd Guest

    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 == (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!!!!!
    }
    red floyd, Nov 20, 2003
    #3
  4. 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.
    =?iso-8859-1?Q?Juli=E1n?= Albo, Nov 20, 2003
    #4
  5. red floyd <> wrote in message news:<yG9vb.392$>...
    > 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 == (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.
    Christian M?rck, Nov 21, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. alanb
    Replies:
    2
    Views:
    519
    alanb
    Apr 23, 2004
  2. Bhushit Joshipura

    defaulting argument to previous argument

    Bhushit Joshipura, Dec 29, 2003, in forum: C++
    Replies:
    5
    Views:
    410
  3. AmitKu
    Replies:
    7
    Views:
    499
    John Timney \(MVP\)
    Jan 8, 2007
  4. Ben Kial
    Replies:
    1
    Views:
    642
    Eric Enright
    Nov 15, 2004
  5. S?ren Gammelmark
    Replies:
    1
    Views:
    1,879
    Eric Sosman
    Jan 7, 2005
Loading...

Share This Page