Reference to member-function of inherited class

Hi,
I have some problems with calling by reference function in inherited class.
here it is:
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
// Base class
class Base
{
public:
dMatrix Base::*nrfunc (double,double,double,double);

dMatrix quad3d(dMatrix (Base::*func)(double, double, double, double), double x1, double x2)
{
	nrfunc=func;
	return qgaus_n_log(&Base::f1,x1,x2);
}

dMatrix f1(double y)
{
	return (this->*nrfunc)(ksav, zsav, y, z_v);
}

void Result()
{
	quad3d(&Base::Integrand,x1,x2);
}

//set virtual - to be defined in inherited class
virtual dMatrix Integrand (double, double, double, double);

dMatrix qgaus_n_log(dMatrix (Base::*integ) (double), double a, double b)
{
//here is just summ
}
}

class Next: public Base
{
public:
dMatrix Integrand (double, double, double, double)
{
// here is definition.
}

}

// then in main(), create Next obj and call Result(); 


Compilator says - its mismatch call - declared dMatrix (Base::*func)(double, double, double, double) but calling dMatrix (Next::*func)(double, double, double, double).

So, my question is - how to make my inherited class to call this bunch of functions without setting them virtual and respecifying their type in inherited class?
This looks pretty weird. Do you really need to do this like this? I mean, you're basically just hand-writing a polymorphic function. Why not just use the polymorphism facilities that the language provides?
Well, i did get why its weird? the idea is - there is integration routine in base class and then in inherited class the integrand function is specified. Without inheritance the code is pretty similar to one from Numerical Recipe textbook, multidimensional integration routine.

What do you mean by
the polymorphism facilities that the language provides?

There is the virtual function or the polimorphism it about it?
Well, i did get why its weird? the idea is - there is integration routine in base class and then in inherited class the integrand function is specified. Without inheritance the code is pretty similar to one from Numerical Recipe textbook, multidimensional integration routine.
Isn't that book written in C? What may be idiomatic in one language may be weird or pointless in another.

C++ has polymorphism built-in. There's no need to mess around with function pointers like this.
Since you already have Base::Integrand() as virtual, you can get rid of nrfunc and all those function pointer arguments.
Then in f1():
1
2
3
4
dMatrix f1(double y)
{
	return this->Integrand(ksav, zsav, y, z_v);
}
Since Base::Integrand() is virtual, Base is polymorphic. When you call p->Integrand(/*...*/) and p is of type 'Base *' (for example, the this pointer inside Base::f1()), the correct version of Integrand() will be called, depending on the run time type of the object.

To illustrate more clearly:
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
#include <iostream>

struct Base{
    virtual ~Base(){}
    void f1(){
        this->f();
    }
    virtual void f(){
        std::cout <<"From Base.\n";
    }
};

struct DerivedA:public Base{
    void f(){
        std::cout <<"From DerivedA.\n";
    }
};

struct DerivedB:public Base{
    void f(){
        std::cout <<"From DerivedB.\n";
    }
};

int main(){
    Base A;
    DerivedA B;
    DerivedB C;
    A.f1();
    B.f1();
    C.f1();
}
Last edited on
Isn't that book written in C?

Yes, in just C, without pluses.

1
2
3
4
dMatrix f1(double y)
{
	return this->Integrand(ksav, zsav, y, z_v);
}


It`s not that thing i would need - in derived class i`ll need to call different Integrand, so i would like to pass by ref corresponding Integrand to multidim-integration bunch.

C++ has polymorphism built-in.

Yep. You are right, before might be i messed with pointers, but now its working.
Nevertheless, thanks.
Last edited on
Topic archived. No new replies allowed.