1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
Class Car{
public:
Usage* usage_calculator;
virtual gasolineEquation(int n,double* a,double* b){};
Car(){usage_calculator=new Usage};
}
Class Audi : public Car
{
public:
void gasolineEquation(int n,double* a,double* b){
for(int i=0;i<n,i++){b[i]=a[i]*3;}
};
}
Class Porsche: public Car
{
public:
void gasolineEquation(int n,double* a,double* b){
for(int i=0;i<n,i++){b[i]=a[i]*10;}
};
}
Class Usage{
public:
typedef void (Car::*funcPtr)(int,double*,double*);
funcPtr ptr2gasolineequation;
Car* consideredcar;
calculate_usage(){
double m[3],n[3];
(*ptr2gasolineequation)(3,m,n);//Call of the characteristic function
};
init(){
//THIS IS THE PROBLEM, AS I CANNOT PASS THE CHILD FUNCTION
//MEMBER ADDRESS TO THE POINTER, AS THE POINTER IS EXPECTING
//THE TYPE: void (Car::*)(int,double*,double*) and not
//eg. void (Porsche::*)(int,double*,double*)
ptr2gasolineequation=&consideredcar[0].gasolineEquation();
}
}
int main(){
Car* carsarray=new Car [2];
carsarray[0]=new Audi;
carsarray[1]=new Porsche;
//Pass the gasolineEquation to the usage_calculator member of the cars
carsarray[0].usage_calculator.init();
carsarray[1].usage_calculator.init();
}
|