473,386 Members | 2,114 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,386 software developers and data experts.

function pointer in class

#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;
}

Sep 18 '06 #1
7 1309
morz wrote:
#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

Sep 18 '06 #2
Thanks,it works!

How about if I dont want using static function,because I want my hello
function can access class member.Thanks.

Sep 18 '06 #3
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);
}
};

Sep 18 '06 #4

morz wrote:
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);
}

Sep 18 '06 #5
Wow ! it works! Thanks a lot.

Sep 18 '06 #6
morz wrote:
Wow ! it works! Thanks a lot.
Firstly I dont understand why the initial code in first mail doesn't
work. Can someone please explain?

Sep 18 '06 #7
On 18 Sep 2006 00:43:10 -0700 in comp.lang.c++,
"bh**************@gmail.com" <bh**************@gmail.comwrote,
>morz wrote:
>Wow ! it works! Thanks a lot.

Firstly I dont understand why the initial code in first mail doesn't
work. Can someone please explain?
Read section "[33] Pointers to member functions" in Marshall Cline's
C++ FAQ. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/

Sep 18 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Roy Yao | last post by:
Hello, I need to pass a pointer to a callback function to the lower level modules. But the function is thought to be a virtual member one. How can I get the real address of the virtual...
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
6
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than calling a virtual function in base class? Thanks
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
5
by: pauldepstein | last post by:
Hi all, I saw some code like this: unsigned short SomeFunc(unsigned short SomeNum, bool SomeBool, const SomeClass& SomeMem, bool(SomeClass::*AmemberFunctionOfSomeClass)(const unsigned...
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.