E
Emanuel Ziegler
Hello,
I want to do some mathematics with functions. In my case the function
classes are very complex, but this simple example has the same problems.
To allow calculations that begin with a double, I have to define a global
operator+ (or -*/) and special function classes.
--- source begins here ---
#include <iostream>
using namespace std;
class function {
// private:
// function *f;
public:
function (function *F = 0) {
cout << "constructor" << endl;
// f = F;
}
virtual ~function () {
cout << "destructor" << endl;
// delete f;
}
virtual double operator() (double x) {
// if ( f == 0 )
cout << "ERROR!" << endl;
// else
// return (*f)(x);
};
};
class identity : public function{
public:
virtual double operator() (double x) { return x; }
};
class sum : public function {
private:
double a;
function *b;
public:
sum (double A, function &B) : function() { a = A; b = &B; }
virtual double operator() (double x) { return a+(*b)(x); }
};
function & operator+ (double x, function &B) {
return *new sum(x,B);
}
int main () {
identity id;
cout << (1+id)(1) << endl;
cout << (1+(1+id))(1) << endl; // comment out for case 3
return 0;
}
--- source ends here ---
There are three cases:
(1) Run the program as it is. You should get:
constructor
constructor
2
constructor
constructor
3
destructor
As you can see, this creates memory leaks!
(2) Comment out all lines in the declaration of function and
change the definition of operator + to
function operator+ ...
return new ...
i.e. remove the first & in the first line and the * in the second line
of the definition.
This program will _not_ compile! On g++ I get
src/prog/testfunc.cpp: In function `int main()':
src/prog/testfunc.cpp:43: error: no match for 'operator+' in '1 +
operator+(double, function&)((&id))'
src/prog/testfunc.cpp:36: error: candidates are: function operator
+(double,function&)
I have no problems with member operators doing calculations like
(function+1)+2
it's only the global operators!
(3) Same as in case 2, but also comment out the marked line in case 3 and
you should get
constructor
constructor
constructor
2
destructor
destructor
destructor
My questions are now:
1. Does anybody know a better solution than case 3 for avoiding memory
leaks?
2. If not, how can I get case 2 to work (without changing int main(),
of course)?
Thanks in advance
Emanuel
I want to do some mathematics with functions. In my case the function
classes are very complex, but this simple example has the same problems.
To allow calculations that begin with a double, I have to define a global
operator+ (or -*/) and special function classes.
--- source begins here ---
#include <iostream>
using namespace std;
class function {
// private:
// function *f;
public:
function (function *F = 0) {
cout << "constructor" << endl;
// f = F;
}
virtual ~function () {
cout << "destructor" << endl;
// delete f;
}
virtual double operator() (double x) {
// if ( f == 0 )
cout << "ERROR!" << endl;
// else
// return (*f)(x);
};
};
class identity : public function{
public:
virtual double operator() (double x) { return x; }
};
class sum : public function {
private:
double a;
function *b;
public:
sum (double A, function &B) : function() { a = A; b = &B; }
virtual double operator() (double x) { return a+(*b)(x); }
};
function & operator+ (double x, function &B) {
return *new sum(x,B);
}
int main () {
identity id;
cout << (1+id)(1) << endl;
cout << (1+(1+id))(1) << endl; // comment out for case 3
return 0;
}
--- source ends here ---
There are three cases:
(1) Run the program as it is. You should get:
constructor
constructor
2
constructor
constructor
3
destructor
As you can see, this creates memory leaks!
(2) Comment out all lines in the declaration of function and
change the definition of operator + to
function operator+ ...
return new ...
i.e. remove the first & in the first line and the * in the second line
of the definition.
This program will _not_ compile! On g++ I get
src/prog/testfunc.cpp: In function `int main()':
src/prog/testfunc.cpp:43: error: no match for 'operator+' in '1 +
operator+(double, function&)((&id))'
src/prog/testfunc.cpp:36: error: candidates are: function operator
+(double,function&)
I have no problems with member operators doing calculations like
(function+1)+2
it's only the global operators!
(3) Same as in case 2, but also comment out the marked line in case 3 and
you should get
constructor
constructor
constructor
2
destructor
destructor
destructor
My questions are now:
1. Does anybody know a better solution than case 3 for avoiding memory
leaks?
2. If not, how can I get case 2 to work (without changing int main(),
of course)?
Thanks in advance
Emanuel