"Ivan Vecerina" <NO**********************************@vecerina.com > wrote in message news:cn**********@newshispeed.ch...
"Alex Vinokur" <al****@big-foot.com> wrote in message
news:30*************@uni-berlin.de... I need something like "function inheritance".
typedef void (*func_type1) (int);
typedef int (*func_type2) (double);
typedef char (*func_type3) (short, int);
...
I need a vector contains pointers to functions of types above.
Because conceptions "base function" and "derived function" are missing in
C++,
it seems that the problem (of creating the vector) should be resolved
by using special base and derived classes/structures.
I have trouble understanding the concept of function inheritance.
To talk about inheritance, you need IMO a common interface between
the 'parent' and 'child'. If the functions, as above, have
incompatible interfaces, how can you interact with the 'child'
as if it were a 'parent' ?
How are the functions above more compatible/related to each
other than any other function ? What makes them a "family" ?
[snip]
It seems that code below is what I want to.
========= foo.cpp : BEGIN =========
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef int (*func_type1) (int);
typedef int (*func_type2) (int, int);
int f1 (int x) { return (10 * x + 1); }
int f2 (int x, int y) { return (10 * x + 10 * y + 2); }
int f3 (int x) { return (10 * x + 3); }
// -------------------------
struct BFunc
{
static map<string, BFunc*> store_s;
static void init_store_fs ();
static void free_store_fs ();
virtual int get_value (int, char**) const = 0;
virtual ~BFunc() {}
};
map<string, BFunc*> BFunc::store_s;
struct DFunc1 : public BFunc
{
const func_type1 func_;
DFunc1 (const func_type1& func_i) : func_ (func_i) {}
int get_value (int argc, char** argv) const
{
assert (argc >= 3);
return func_ (atoi (argv[2]));
}
};
struct DFunc2 : public BFunc
{
const func_type2 func_;
DFunc2 (const func_type2& func_i) : func_ (func_i) {}
int get_value (int argc, char** argv) const
{
assert (argc >= 4);
return func_ (atoi (argv[2]), atoi (argv[3]));
}
};
// ------
const string name1 ("a1");
const string name2 ("a2");
const string name3 ("a3");
void BFunc::init_store_fs ()
{
store_s[name1] = new DFunc1 (f1);
store_s[name2] = new DFunc2 (f2);
store_s[name3] = new DFunc1 (f3);
}
void BFunc::free_store_fs ()
{
for (map<string, BFunc*>::iterator pos_iter = store_s.begin();
pos_iter != store_s.end();
pos_iter++
)
{
delete pos_iter->second;
}
}
int main (int argc, char** argv)
{
BFunc::init_store_fs ();
assert (argc >= 2);
const string name(argv[1]);
if (BFunc::store_s.count(name))
{
cout << BFunc::store_s[name]->get_value (argc, argv) << endl;
}
else
{
cout << name << " is not in store" << endl;
}
BFunc::free_store_fs ();
return 0;
}
========= foo.cpp : END ===========
========= Compilation & Run : BEGIN =========
$ gpp --version
gpp.exe (GCC) 3.4.1
[---omitted---]
$ gpp -W -Wall foo.cpp
// No errors/warnings
$ a.exe a1 3 // function f1 is invoked; its type is func_type1
31
$ a.exe a2 3 4 // function f2 is invoked; its type is func_type2
72
$ a.exe a3 3 // function f3 is invoked; its type is func_type1
33
$ a.exe a4
a4 is not in store
========= Compilation & Run : END ===========
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn