473,385 Members | 1,757 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,385 software developers and data experts.

Function Pointer as a class member

I am trying to call function pointer using class pointer and compiler
is not allowing it.

//================================================== =
#include <stdio.h>
class TestClass
{
public:
int func1(int a) {
printf("\nTestClass:func1 a = %d\n", a);
return a;}
TestClass() { fptr = &TestClass::func1; }
int (TestClass::*fptr)(int);
};
int main()
{
TestClass* T1 = new TestClass;
int result = (T1->*fptr)(4); // compiler says undeclared identifier
delete T1;
return 0;
}

whereas at the same it works

#include <stdio.h>

class TestClass
{
public:
int func1(int a) {
printf("\nTestClass:func1 a = %d\n", a);
return a; }
TestClass() {
fptr = &TestClass::func1; }
int (TestClass::*fptr)(int);
int fptrwrapper(int a) {
(this->*fptr)(a);
return 0;}
};

int main()
{
TestClass* T1 = new TestClass;
T1->fptrwrapper(4);
delete T1;
return 0;
}

could you please point out where i am making mistake.. ???
Jul 23 '05 #1
2 1171
"Pramod Sharma" <pr********@yahoo.com> wrote in message
news:92**************************@posting.google.c om...
I am trying to call function pointer using class pointer and compiler
is not allowing it.

//================================================== =
#include <stdio.h>
class TestClass
{
public:
int func1(int a) {
printf("\nTestClass:func1 a = %d\n", a);
return a;}
TestClass() { fptr = &TestClass::func1; }
int (TestClass::*fptr)(int);
};
int main()
{
TestClass* T1 = new TestClass;
int result = (T1->*fptr)(4); // compiler says undeclared identifier

int result = (T1->*T1->fptr)(4);
delete T1;
return 0;
}


DW

Jul 23 '05 #2
I think your concept is wrong. Class has a point that don't see it. It's
"this" point.You declared point to Class.You can use T1 to directly visit
func1. Don't use fptr
I wrote this progamme.I hope you understand.

#include <iostream>
#include <stdlib.h>
using namespace std;
class TestClass
{
public:
int func1(int a)
{
cout << "TestClass:func1 a ="<< a;
return a;
}
};

int main()
{
TestClass *T1=new TestClass;
int result = (T1->func1)(4); // compiler says undeclared identifier
delete T1;

system("PAUSE");
return 0;
}

This programme use STL and impelment in visual c++.net.
=========================================
"Pramod Sharma" <pr********@yahoo.com> ????
news:92**************************@posting.google.c om...
I am trying to call function pointer using class pointer and compiler
is not allowing it.

//================================================== =
#include <stdio.h>
class TestClass
{
public:
int func1(int a) {
printf("\nTestClass:func1 a = %d\n", a);
return a;}
TestClass() { fptr = &TestClass::func1; }
int (TestClass::*fptr)(int);
};
int main()
{
TestClass* T1 = new TestClass;
int result = (T1->*fptr)(4); // compiler says undeclared identifier delete T1;
return 0;
}

whereas at the same it works

#include <stdio.h>

class TestClass
{
public:
int func1(int a) {
printf("\nTestClass:func1 a = %d\n", a);
return a; }
TestClass() {
fptr = &TestClass::func1; }
int (TestClass::*fptr)(int);
int fptrwrapper(int a) {
(this->*fptr)(a);
return 0;}
};

int main()
{
TestClass* T1 = new TestClass;
T1->fptrwrapper(4);
delete T1;
return 0;
}

could you please point out where i am making mistake.. ???

Jul 23 '05 #3

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

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
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...
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...
15
by: Albert | last post by:
Hi, I need to pass a pointer-to-member-function as a parameter to a function which takes pointer-to-function as an argument. Is there any way to do it besides overloading the function? Here...
6
by: gustav04 | last post by:
hi all i have a question: what is the difference between a c-function and an c++ class method (both do exactly the same thing). lets say, i have a function called print2std() and a class...
7
by: jon wayne | last post by:
Hi I'm a little confused here about the lifetime of a static pointer to member function, Say, I declare,define & initialize a static ptr to mem function in the header file of a class(the class...
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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.