Passing Polymorphic Pointer to Pointer into Function

In trying to pass a pointer of pointers from one function to another, I lose my objects somewhere.



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

using namespace std;

class function
{
public:
    virtual double operator () (double x) {return 1.5;}
};

class pwfunction : public function
{
public:
    virtual double operator() (double x) {return 2.0;}
};

void interface();
void definefuncs (function** funcs, long unsigned numfuncs);
void interpolate(function* infunc);
void solvefuncs(function** funcs, long unsigned numfuncs);

int main()
{
    interface();
    return 0;
}

void interface()
{
    long unsigned numfuncs = 1;
    function* funcs[numfuncs];

    definefuncs(funcs, numfuncs);

    solvefuncs(funcs, numfuncs);
}

void definefuncs (function** funcs, long unsigned numfuncs)
{
    interpolate(funcs[0]);
}

void interpolate(function* infunc)
{
    infunc = new pwfunction();
    cout<< (*infunc)(1.5)<<endl;    //works
}

void solvefuncs(function** funcs, long unsigned numfuncs)
{
    cout<< (*funcs[0])(1.5);    //Error Message: Segmentation fault
}
1
2
3
4
5
void interpolate(function* infunc) //pass a copy of the pointer
{
    infunc = new pwfunction(); //modify the copy
    //cout<< (*infunc)(1.5)<<endl;    //learn to use a debugger
}//leak 
Topic archived. No new replies allowed.