Click here to Skip to main content
15,913,090 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
 double getSalary(int v,double s){return v*s;}
 double getSalary(double s){return s/2;}
 double getSalary(){return 1000;}

class person{        //the class is not inherited, so I can't use virtual 
public:
	char name[20];
        int age;
	double salary;

 person(char* num="Anonim",int a=0,double s=0):age(a),salary(s)
{	
	strcpy(name,num);
}
//...methods...
       double (*person::getSalary)() //I have also tried without personand many others...
};
void main(){
   person p("John",25,3000);
   cout<<"Salary is "<<p.getSalary  /*not sure that this is the right way to call  it, but i don't think this is the main mistake, also please correct me here*/
}


Please tell me how to declare inside the class a pointer to getSalary(),in this example I want the 3rd version of the function.I thought that the compiler automatically knows wich one of the 3 I want to call, baseing on function's signature.I have no compileing error but it brakes on run-time.I have tried to debug it, it seams that the pointer is empty.
Posted
Updated 7-Aug-11 2:43am
v2

You do not initialize the function pointer person::getSalary. Dereferencing this pointer leads to a crash.

Use p.getSalary= getSalary; before invoking p.getSalary(); hoping that the compiler will know what overload to pick.
 
Share this answer
 
Hi,

Your example will always should call
getSalary()
only.

Because your function pointer is of type
(*person::getSalary)()
where there is no parameters.

I have not tested it with the real code , but this is what logically implies.
 
Share this answer
 
Comments
Alex_RO 8-Aug-11 7:01am    
Yes, but if you change it with (*person::getSalary)(double s), or with (*person::getSalary)(int v,double s), will work, anyway I would like a pointer that would work for any version of the function,if it's possible.
Alex_RO 8-Aug-11 7:18am    
I posted an improved solution that work, but I don't know why, it was deleted.
There are several things wrong with the example, but I will stick with the question asked.

The declaration "double (*person::getSalary)();" should not have compiled.

The declaration of a pointer to a member function would be
double (person::*getSalary)();.

Since your code example implies you want to point to a global function the correct declaration would be
double (*getSalary)();.

The following should give you the general idea.

C#
#include <cstring>
double getSalary(int v, double s) { return v * s; }
double getSalary(double s) { return s/2; }
double getSalary() { return 1000; }

class person
{
public:
    char name[20];
    int age;
    double salary;

    person(char* num="Anonim",int a=0, double s=0) : age(a), salary(s)
    {
        strcpy(name,num); // dangerous: length of num is unknown
        getSalary = ::getSalary;
        getIt = ::getSalary;
    }
    // pointer to global function with signature of
    // double (*)()
    double (*getSalary)();
    // same thing with a different varaible name for the pointer
    double (*getIt)();
};

void test()
{
    person p("John",25,3000);
    std::cout << "Salary is " << p.getSalary() << std::endl;
    std::cout << "Salary is " << p.getIt() << std::endl;
}
 
Share this answer
 
Comments
Alex_RO 10-Aug-11 15:07pm    
An interested solution, I will try it to see what I get. Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900