C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

function pointer in class

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
morz
Guest





PostPosted: Mon Sep 18, 2006 8:32 am    Post subject: function pointer in class Reply with quote



#include <cstdlib>
#include <iostream>

using namespace std;
what wrong with my code? how to correct this code to make it works?
Thanks.


class test {
public:
void hello() {
cout << "hello" << endl;
}

void ok(void (*j)()) {
(*j)();
}

void callme() {
ok(hello);
}
};

int main(int argc, char *argv[]) {
test k;
k.callme();
system("PAUSE");
return 0;
}
Back to top
Thomas Tutone
Guest





PostPosted: Mon Sep 18, 2006 8:41 am    Post subject: Re: function pointer in class Reply with quote



morz wrote:
Quote:
#include <cstdlib
#include <iostream

using namespace std;
what wrong with my code? how to correct this code to make it works?
Thanks.


class test {
public:
void hello() {

Change the above line to:

static void hello() {

Best regards,

Tom
Back to top
morz
Guest





PostPosted: Mon Sep 18, 2006 9:00 am    Post subject: Re: function pointer in class Reply with quote



Thanks,it works!

How about if I dont want using static function,because I want my hello
function can access class member.Thanks.
Back to top
morz
Guest





PostPosted: Mon Sep 18, 2006 9:09 am    Post subject: Re: function pointer in class Reply with quote

I try doing like this,but still error :(

class test {
public:
void hello() {
cout << "hello" << endl;
}

void ok(void (test::*j)()) {
(*j)();
}

void callme() {
ok(&hello);
}
};
Back to top
bharath.donnipad@gmail.co
Guest





PostPosted: Mon Sep 18, 2006 9:10 am    Post subject: Re: function pointer in class Reply with quote

morz wrote:
Quote:
Wow ! it works! Thanks a lot.

Firstly I dont understand why the initial code in first mail doesn't
work. Can someone please explain?
Back to top
Guest






PostPosted: Mon Sep 18, 2006 9:10 am    Post subject: Re: function pointer in class Reply with quote

morz wrote:
Quote:
I try doing like this,but still error :(

class test {
public:
void hello() {
cout << "hello" << endl;
}

void ok(void (test::*j)()) {
(*j)();
}

void callme() {
ok(&hello);
}
};

Pointer to member functions are different from regular function
pointers.

change the code to...

void ok(void (test::*j)()) {
(this->*j)();
}

void callme() {
ok(&test::hello);
}
Back to top
morz
Guest





PostPosted: Mon Sep 18, 2006 9:10 am    Post subject: Re: function pointer in class Reply with quote

Wow ! it works! Thanks a lot.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.