but if I try to include these functions in a class like this:
1 2 3 4 5 6 7 8 9 10
class T{
double Multiply(void) {return 1.0;} ;
double (*GetFPointer(void))(void) {
return &Multiply;
}
};
int main(){
return 0;
}
I get the following compilation error:
1 2 3
check.cc: In member function ‘double (* T::GetFPointer())()’:
check.cc:4: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say ‘&T::Multiply’
check.cc:4: error: cannot convert ‘double (T::*)()’ to ‘double (*)()’ in return
Function pointers to class methods have an implied this parameter, making them different from a normal function. You need to change you function pointer to reflect that.
The error message basically says all.
You cannot call a non-static member function without an object.
In this case, it would make sense to make the functions static or rather to put them into a namespace instead of a class.
class T
{
public:
double Multiply(void) {return 1.0;}
typedefdouble (T::*fptr)(void); // typedef for better readability
fptr GetFPointer(void)
{
return &T::Multiply;
}
};
int main()
{
T t;
T::fptr p;
p = t.GetFPointer(); // get function pointer
(t.*p)(); // call function pointer
return 0;
}
#include <iostream>
usingnamespace std;
class T{
public:
double Multiply(double var) {return var*2.0;} ;
double Divide(double var) {return var/2.0;} ;
typedefdouble (T::*fptr)(double);
fptr GetFpointer(bool arg){
if (arg) return &T::Multiply;
elsereturn &T::Divide;
}
// code which sets a function called Operation
set_function(bool mult){
if(mult) {/*Operation is set to multiply*/};
else {/*Operation is set to divide*/};
}
};
int main(){
//something like this will be nice
T t;
t.set_function(1);
cout<< t.Operation(40)<<endl;
return 0;
}
I am trying to implement the above code by m4ster r0shi in a group code for a particular class "Class_Name". Inside my class declaration I have these statements
And outside the class declaration, I have these lines
1 2 3 4 5 6 7 8 9 10 11 12 13
void Class_Name::wall(constint B){
/* description of wall function */
}
void Class_Name::no_wall(constint B){
/* description of no_wall function */
}
const Clas_Name::fptr Class_Name::set_wall[]=
{
&Class_Name::no_wall,
&Class_Name::wall
};
I use a makefile to compile this group code. And all the individual files compile fine, but while linking the files I get the following error message at the very end of the compiling process
1 2
'Filename.o:(.rodata+0x72c): multiple definition of `Class_Name::set_wall'
Filename.o:(.rodata+0x10): first defined here
inside a header file that you include more than one times in your code. Try removing it from here (from the header) and putting it in a separate cpp file (in which you will also include your header of course).
Is it possible to make the SetOperation function in the original code by "m4ster r0shi" static? I am getting compile time errors with that. Anything logically wrong with that?
Basically, I just want to "SetOperation()" once in the very beginning of the run depending on some input parameters and not touch it again. Is it possible to do it without actually initializing an object of the class?