By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,191 Members | 774 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,191 IT Pros & Developers. It's quick & easy.

Function Pointer as a class member

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.