473,385 Members | 1,548 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.

How to declare and use a function pointer?


/*********************

**********************/
I'm trying to declare a function pointer in class A and set it in class B, but I get
syntax errors. How can I get this to compile and run?

thanks,
Sam

class A {
public:
A(void) {
}
virtual ~A() {
}
void (*func)(int i, bool b); // trying to declare a function pointer
};

/*********************

**********************/

class B {

public:
B(void) {
a = new A();

a->func = &func(int i, bool b); // <<<<<<<<<< syntax error <<<<<<<<<<<<<<

// Microsoft visual studio error messages about the above line:
// error C2144: syntax error : missing ')' before type 'int'
// error C2660: 'func' : function does not take 0 parameters
// error C2059: syntax error : ')'

}
virtual ~B() {
delete a;
}
A *a;
void func(int i, bool b) {
return;
}

};

/************************************************** *************************
I need to have this so that different 'B'-type classes can
change the behavior of A::func
************************************************** *************************/

int main(int argc, char *argv[]) {
B *b = new B();

b->a->func(99, true); // should call B::func()

delete b;

return 0;
}
Jul 14 '06 #1
2 2069

Sam Waller wrote:
/*********************

**********************/
I'm trying to declare a function pointer in class A and set it in class B, but I get
syntax errors. How can I get this to compile and run?

thanks,
Sam

class A {
public:
A(void) {
}
virtual ~A() {
}
void (*func)(int i, bool b); // trying to declare a function pointer
};

/*********************

**********************/

class B {

public:
B(void) {
a = new A();

a->func = &func(int i, bool b); // <<<<<<<<<< syntax error <<<<<<<<<<<<<<
You can't make a->func point to a member of B, unless you make it a
static function.
See also:
http://www.parashift.com/c++-faq-lit...o-members.html

Jul 14 '06 #2
"Sam Waller" <sa********@avmet.comwrote in message
news:u0********************************@4ax.com...
>
/*********************

**********************/
I'm trying to declare a function pointer in class A and set it in class B,
but I get
syntax errors. How can I get this to compile and run?

thanks,
Sam

class A {
public:
A(void) {
}
virtual ~A() {
}
void (*func)(int i, bool b); // trying to declare
a function pointer
};

/*********************

**********************/

class B {

public:
B(void) {
a = new A();

a->func = &func(int i, bool b); // <<<<<<<<<< syntax error
<<<<<<<<<<<<<<

// Microsoft visual studio error messages about the above line:
// error C2144: syntax error : missing ')' before type 'int'
// error C2660: 'func' : function does not take 0 parameters
// error C2059: syntax error : ')'

}
virtual ~B() {
delete a;
}
A *a;
void func(int i, bool b) {
return;
}

};

/************************************************** *************************
I need to have this so that different 'B'-type classes can
change the behavior of A::func
************************************************** *************************/

int main(int argc, char *argv[]) {
B *b = new B();

b->a->func(99, true); // should call B::func()

delete b;

return 0;
}
Well if you absolutely must do this for whatever capricious reason(!), then:

#include <iostream>

template <typename T>
class A
{
public:
void (T::*func)(int i, bool b);
};

class B
{
public:
B()
{
a = new A<B>;
a->func = &B::func;
}

~B()
{
delete a;
}

A<B*a;

void func(int i, bool b)
{
std::cout << "func(" << i << ", " << b << ")\n";
}
};

int main()
{
B b;
(b.*(b.a->func))(99, true);
return 0;
}

But it's really, really dubious. What are you actually trying to achieve
here? If you really want to have different func behaviour in various
classes, what's wrong with something like

class A
{
public:
virtual void func(int i, bool b) = 0;
};

class B : public A
{
public:
void func(int i, bool b)
{
// Whatever
}
};

?

I'm not really clear what you're after here.

Hope this helps a bit! :)
Stu
Jul 14 '06 #3

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

Similar topics

0
by: Walti | last post by:
Hi I'm trying to interface a foreign DLL that is written in Delphi. I know: - Delphi-Integer is a 32-Bit signed Integer --> VB: LONG - Delphi-PChar is a Pointer to Zero-Terminated ASCII String...
9
by: Dave | last post by:
I am trying to call VerQueryValue from a C# program. VerQueryValue takes as one of its parameters a pointer to a pointer to an array of bytes, which it uses to return a pointer to the required...
6
by: **Developer** | last post by:
Notice below I sometimes used the "A" version. I found by cut-and-try that only the "A" version would work correctly. Anyone have a suggestion of why the "W" version would not work correctly? ...
0
by: eric | last post by:
Can I declare a pure virtual member function which accepts as input a boost shared pointer to an object of a base class, such that - concrete implementations of the function can redirect the...
2
by: Tany | last post by:
How can I declare function returning array of Integer pointers . Please help !!
6
by: CJ | last post by:
Functions can accept "argv like" variable definitions, i.e. foo(int argc, char *argv), which was defined/initialized in the C start up stubs, but we can't declare one for our own use. Example: ...
5
by: benben | last post by:
How do you declare a function f that takes a parameter that is a pointer to itself? So you can do f(f); Pardon my curiosity! Ben
1
by: Angus | last post by:
Hello I have a function in a C DLL like this: DWORD __stdcall FCSConnect(const TCHAR * ServerName, UINT ServerPort, FCS_HANDLE * FCSHandle); My interpretation in VB is (VB6):
2
by: Sreeramu | last post by:
Declare a pointer to a function which takes array of pointer to a function as an argument and return the pointer to a function..... how to declare this....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: 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...

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.