C++ und function pointers
Today I have revealed an interesting thing trying to implement a simple progress bar (file reading). So the thing is:
if we'd like to make use of the following class:
class FileReader {
public:
int readBinaryFile(const char*, Data&, void (*)(long)) const;
};
The only thing it does - reads the given file and converts the data read into the Data object. So I tried to use it from the other object of the following class:
class FileReaderUser {
Data *data;
private:
void showProgressBar(long);
public:
void openFile();
};
void FileReaderUser::openFile(){
.....
FileReader reader;
reader.readBinaryFile((const char*) fileName, *data, &(FileReaderUser::showProgressBar);
.....
}
and ... it doesn't work. Why?
The cause is: the function signature
void (*FileReaderUser::showProgressBar)(long);
differs from
void (*)(long);
So I found the following topic on these theme:
http://www.parashift.com/c++-faq-lite/pointers-to-members.html
------------------------------------------------------------------------------
[33.1] Is the type of "pointer-to-member-function" different from "pointer-to-function"?
Yep.
Consider the following function:
int f(char a, float b);
The type of this function is different depending on whether it is an ordinary function or a non-static member function of some class:
Its type is "int (*)(char,float)" if an ordinary function
Its type is "int (Fred::*)(char,float)" if a non-static member function of class Fred
Note: if it's a static member function of class Fred, its type is the same as if it were an ordinary function: "int (*)(char,float)".
------------------------------------------------------------------------------
So simply declare the function as "static" and the code works!!!
if we'd like to make use of the following class:
class FileReader {
public:
int readBinaryFile(const char*, Data&, void (*)(long)) const;
};
The only thing it does - reads the given file and converts the data read into the Data object. So I tried to use it from the other object of the following class:
class FileReaderUser {
Data *data;
private:
void showProgressBar(long);
public:
void openFile();
};
void FileReaderUser::openFile(){
.....
FileReader reader;
reader.readBinaryFile((const char*) fileName, *data, &(FileReaderUser::showProgressBar);
.....
}
and ... it doesn't work. Why?
The cause is: the function signature
void (*FileReaderUser::showProgressBar)(long);
differs from
void (*)(long);
So I found the following topic on these theme:
http://www.parashift.com/c++-faq-lite/pointers-to-members.html
------------------------------------------------------------------------------
[33.1] Is the type of "pointer-to-member-function" different from "pointer-to-function"?
Yep.
Consider the following function:
int f(char a, float b);
The type of this function is different depending on whether it is an ordinary function or a non-static member function of some class:
Its type is "int (*)(char,float)" if an ordinary function
Its type is "int (Fred::*)(char,float)" if a non-static member function of class Fred
Note: if it's a static member function of class Fred, its type is the same as if it were an ordinary function: "int (*)(char,float)".
------------------------------------------------------------------------------
So simply declare the function as "static" and the code works!!!
Comments