Passing child member functions to other classes

Hey,
I have a question depending the passing of a child member function to a another class method. In the follwing I tried to show the used code structure.
The problem is, that if I use typedef void (Car::*funcPtr)(int,double*,double*) within Usage to store the gasoline functions of Audi or Porsche. As Error I get: error: cannot convert...
I commented the corresponding lines in the code.
Any ideas how to solve this conversion issue?
Thanks a lot in advance

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();

}
Last edited on
Passing a pointer to the derived class member function is unnecessary.
virtual gasolineEquation(int n,double* a,double* b) is virtual (run-time polymorphic). If we call it on a reference (or a pointer) to Car, the overridden function in the derived class would be called.

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
#include <iostream>
#include <string>

struct Car {

    struct usage_calculator_t {

        const Car& considered_car ;
        usage_calculator_t( const Car& car ) : considered_car(car) {}
        void calculate_usage( int n, const double* a, double* b ) const
        { for( int i = 0 ; i < n ; ++i ) considered_car.gasoline_equation( a[i], b[i] ) ; }
    };

    const usage_calculator_t usage_calculator ;

    Car() : usage_calculator(*this) {}
    virtual ~Car() = default ;

    virtual void gasoline_equation( double a, double& b) const = 0 ;
    virtual std::string name() const = 0 ;
};

struct Audi : public Car
{
    virtual void gasoline_equation( double a, double& b) const override { b =  a * 0.2 ; }
    virtual std::string name() const override { return "Audi Spyder" ; }
};

struct Porsche : public Car
{
    virtual void gasoline_equation( double a, double& b) const override { b = a * 0.35 ; }
    virtual std::string name() const override { return "Porsche Panamera" ; }
};

int main()
{
    Audi audi ;
    Porsche porche ;

    const Car* cars[] { std::addressof(audi), std::addressof(porche) };

    const int n = 3 ;
    const double a[n] { 13.6, 44.4, 82.1 } ;
    double b[n] {} ;

    for(  const Car* car : cars )
    {
        std::cout << car->name() << ":\n" ;
        car->usage_calculator.calculate_usage( n, a, b ) ;
        for( int i = 0 ; i < n ; ++i ) std::cout << a[i] << " kms " << b[i] << " litres\n" ;
        std::cout << "----------------\n" ;
    }
}

http://coliru.stacked-crooked.com/a/d0f2554e737cf7f0
Topic archived. No new replies allowed.