473,657 Members | 2,594 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1833
"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_cas t (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)(cla ss ThisClassDoesNo tExist&);
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************ *************** *******@vecerin a.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_sto re_fs ()
{
store_s[name1] = new DFunc1 (f1);
store_s[name2] = new DFunc2 (f2);
store_s[name3] = new DFunc1 (f3);
}
void BFunc::free_sto re_fs ()
{
for (map<string, BFunc*>::iterat or pos_iter = store_s.begin() ;
pos_iter != store_s.end();
pos_iter++
)
{
delete pos_iter->second;
}
}

int main (int argc, char** argv)
{
BFunc::init_sto re_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_sto re_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,shar ed_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
4641
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 class version from the base class pointer and still be able to use a vector with derived element pointers? class BaseElem { }; class DerivedElem1 { };
8
1849
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 what arguments do you decide whether to pass a reference to the object of class B to the member function like this operator()(classB& objB) or to have in class A a data member (a const pointer to a class B object or so) and have this set during...
37
4986
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 signature. When developing this lib, I figured that the pointer-to-member-function, although seemingly an attractive solution, does not work well for us.
6
4401
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 same invocation from a DistantlyRelated class. What actually happened was that the compiler produced: error C2247: 'A::function' not accessible because 'CloselyRelated' uses 'private' to inherit from 'A' I'm guessing that the above compiler...
41
2547
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 += arguments;
2
1754
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 Common(Some_String as String) As String ... End Function
2
3038
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
1394
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 object -- the semantic is that the returned object prototypally inherits from the argument 'o'. function object(initializer, o) { if (!o) o = Object; function f() {} f.prototype = o;
4
2102
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) : x0(a), y0(b),
0
8392
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8730
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1950
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.