473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Function inheritance

Hi,

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.

struct BFunc
{
// Stuff
};
struct DFunc1
{
func_type1 func_;
DFunc1 (func_type1 f) : func_ (f) {}
// Stuff
};

struct DFunc2
{
func_type2 func_;
DFunc2 (func_type2 f) : func_ (f) {}
// Stuff
};

struct DFunc3
{
func_type3 func_;
DFunc3 (func_type3 f) : func_ (f) {}
// Stuff
};

Now, we have

vector<*BFunc> v;

And, for instance
v.push_back (new DFunc2 (f));
v.push_back (new DFunc1 (ff));
v.push_back (new DFunc3 (fff));
v.push_back (new DFunc2 (ffff));
v.push_back (new DFunc1 (fffff));
The questions are:
1) Do we have to use dynamic_cast (while processing vector<*BFunc>)
to call a relevant function via v[i]?
2) Is there any better solution of the "function inheritance" problem?

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 22 '05 #1
4 1824
"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: creating wrapper classes]
The questions are:
1) Do we have to use dynamic_cast (while processing vector<*BFunc>)
to call a relevant function via v[i]?
2) Is there any better solution of the "function inheritance" problem?


All (non-member) function pointers can safely be converted
to e.g. void(*)() and back. IIRC this may require
a reinterpret_cast (not just a static cast) though,
and is safe if you don't call the function until the
pointer is cast back to its original type.
Rather than void(*)(), it might therefore be safer to
use a function that you won't be able to call (e.g.
because it takes an undefined type as a parameter).

What I would use is:

typedef void (*any_func)(class ThisClassDoesNotExist&);
std::vector<any_func> myFunctions;
hth,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com

Jul 22 '05 #2
* Alex Vinokur:
Hi,

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.
As Ivan Vecerina wrote, the above seems to be unrelated function
signatures.
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.

struct BFunc
{
// Stuff
};
struct DFunc1
{
func_type1 func_;
DFunc1 (func_type1 f) : func_ (f) {}
// Stuff
};
Why not make the function itself a member function?

operator() is provided for that purpose.

Then an object of this class will be a functor object.
struct DFunc2
{
func_type2 func_;
DFunc2 (func_type2 f) : func_ (f) {}
// Stuff
};

struct DFunc3
{
func_type3 func_;
DFunc3 (func_type3 f) : func_ (f) {}
// Stuff
};

Now, we have

vector<*BFunc> v;

And, for instance
v.push_back (new DFunc2 (f));
v.push_back (new DFunc1 (ff));
v.push_back (new DFunc3 (fff));
v.push_back (new DFunc2 (ffff));
v.push_back (new DFunc1 (fffff));
The questions are:
1) Do we have to use dynamic_cast (while processing vector<*BFunc>)
to call a relevant function via v[i]?
For that you need at least one virtual function.

2) Is there any better solution of the "function inheritance" problem?


It's difficult to see what you're trying to achieve since your
function signatures are all different (need different arguments).

What are you intending to use this for?

Most probably that's where the real problem/solution is, whereas this
"function inheritance" stuff is probably particular to an imagined but
ungood solution.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #3

"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


Jul 22 '05 #4
"Alex Vinokur" <al****@big-foot.com> wrote in message
news:30*************@uni-berlin.de...
It seems that code below is what I want to. [a command line utility that uses argv[1] to specify
a function to be called, which takes the rest of
its integer parameters from the command line]

I'd rather wrap things the other way around.
Use the following common function signature:
typedef int (*FuncPtr)( int argc, char** argv );
Or even safer:
typedef int (*FuncPtr)( vector<string> const& params );

Where the vector of parameters can be initialized
in main() as follows:
vector<string> params( argv+2 , argv+argc );

Then provide one or more utility function to avoid
code duplication between the implementations of FuncPtr.
Such as:
int getIntParam( vector<string> const& params, int pos )
{
if(pos>=params.size() )
{ cout<<"...error description..."; exit(1); }
return atoi (params[pos].c_str()); // extra error checks here?
}

struct BFunc
{
static map<string, BFunc*> store_s;
static void init_store_fs ();
static void free_store_fs (); Requiring explicit de-init is bug-prone.
I would replace the three static members by a single non-member
function:
map<string,shared_ptr<BFunc> > getFunctionsMap();
// returns instance whose lifetime is used by the caller.
Using the alternative approach I suggested, this wrapper would
disappear and each function would become something like:
int f1 ( vector<string> const& params )
{
return (10 * getIntParam(0) + 1);
}

Then you do not need a container of polymorphic items anymore:
map<string, FuncPtr> getFunctionMap();

I think you get a cleaner code by keeping the extra parameters
as a string list.
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); assert is not the error checking strategy one would normally
use to check the validity of user(/external) inputs.
return func_ (atoi (argv[2]));
}
};



Regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Sat | last post by:
Hi, I have a simplified version of a problem that I am facing (hope I haven't oversimplified it). This code doesn't work now and I want to find how I can make it work. Can I call the derived...
8
by: Gert Van den Eynde | last post by:
Hi all, I have a question on interface design: I have a set of objects that are interlinked in the real world: object of class A needs for example for the operator() an object of class B. On...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
6
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the...
41
by: Telmo Costa | last post by:
Hi. I have the following code: -------------------------------------- function Tunnel() { //arguments(???); } function Sum() { var sum = 0; for (i=0; i<arguments.length; i++) sum +=...
2
by: Maxwell_Smart | last post by:
Is there a way for a function to refer to itself generically? I'd like to use such a thing (should it exist) for convenience and consistency, not functionality. For example: Function...
2
by: junw2000 | last post by:
In the following code: #include <iostream> using namespace std; class V { public: int i; virtual void f() { cout << "V::f()" << endl;}
3
by: Jeff Stewart | last post by:
I've been working with the JavaScript Shell in Firefox on a mad-scientist problem I've had in my head. Assume a function named 'object' that is designed to create an object based on a prototype...
4
by: zaeminkr | last post by:
I got a good answer here I have still confusing part. I have two very simple classes class DRect { private : double x0, y0, x1, y1; public : DRect(double a, double b, double c, double d) :...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.