473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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

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
4 1870
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: hermit_crab67 | last post by:
Can someone explain to a C newbie why this doesn't work as I expect it to work? (expectations clearly outlined in the printf statement in main routine) OS: Linux 2.4.26 GCC: 2.95.4 void...
3
by: Daniel Diehl | last post by:
Good morning! Now I'm sitting over 2 hours on a problem calling a method in the logitech SDK DLL. Everything is working fine, but calling one method I fell in a problem. I'm not that old with C#...
6
by: | last post by:
Hi all, I have a bunch of dropdownlists that are populated in client-side javascript. When i do a postback I get the following error:- Invalid postback or callback argument. Event...
4
by: Ewald Hofman | last post by:
I have a very strange typing problem: There is the following class Public Class MyBaseClass Implents MyInterface Public Property MyProperty As MyClass implements MyInterface.MyProperty ......
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
5
by: Martin Chen | last post by:
I have a frame set (as per MS FrontPage 2000). It has a contents and a main frame. The contents frame has a menu bar written with with javascript (in the context of a table). In IE6.1 everything...
6
by: ged | last post by:
Hi, i am a oo (c#) programmer, and have not used javascript for a while and i cant work out how javascript manages its references. Object References work for simple stuff, but once i have an...
5
by: Fred.Grieco | last post by:
Hi every body, I have a little pb and I'm turning around : function MyFCTN(var1,var2) { var mytable = document.getElementById("myTBL"); for (var i=myTBL.childNodes.length-1; i>0; i--){...
0
by: parekh | last post by:
Hi All , I am facing a problem wherein my VB project is not recognizing a change in the argument list of a method ( the method itself being declared and defined in another VC++ project and it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.