473,386 Members | 1,799 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.

Can I write a recursive type definition ?

[Cross-posted to: comp.lang.c, comp.lang.c++]

Hello all

I'm facing a very strange problem. I need to define a function that takes as
argument something like a pointer to itself. It takes as argument a pointer
of the same type as a pointer to itself and with possibly other
pointer-to-function value. But I don't have a data type for such a pointer
because the function prototype is in the same time being defined. I hope you
are following me ...

Actualy I need to define a pointer to a function. Different modules of my
app will install a new function to be pointed by this pointer, and each new
function will remember the address of and call the previous function,
forming a chain that exists only in code. I guess the idea is used for
at_exit() function in the library or something like it.

Now I want the function to take as argument a pointer to another instance of
the same function type. This will allow for removing functions from the
chain even in a different order than the one functions were installed, by
having each installed function recognize it's own address in the argument
and then return as a result the previous installed function that it knows of

I hope you are following me...

So how could I declare the prototype for a function that takes as argument
and that returns a pointer to the same function type being declared ?

Thank you
Timothy Madden
Romania
--------------------------------------------------
And I don't wanna miss a thing
Nov 14 '05 #1
13 2560

"Timothy Madden" <ba****@rmv.spam.home.ro> wrote in message
news:2s*************@uni-berlin.de...
[Cross-posted to: comp.lang.c, comp.lang.c++]

Hello all

I'm facing a very strange problem. I need to define a function that takes as argument something like a pointer to itself. It takes as argument a pointer of the same type as a pointer to itself and with possibly other
pointer-to-function value. But I don't have a data type for such a pointer
because the function prototype is in the same time being defined. I hope you are following me ...

Actualy I need to define a pointer to a function. Different modules of my
app will install a new function to be pointed by this pointer, and each new function will remember the address of and call the previous function,
forming a chain that exists only in code. I guess the idea is used for
at_exit() function in the library or something like it.

Now I want the function to take as argument a pointer to another instance of the same function type. This will allow for removing functions from the
chain even in a different order than the one functions were installed, by
having each installed function recognize it's own address in the argument
and then return as a result the previous installed function that it knows of
I hope you are following me...

So how could I declare the prototype for a function that takes as argument
and that returns a pointer to the same function type being declared ?

Thank you
Timothy Madden
Romania
--------------------------------------------------
And I don't wanna miss a thing


I believe that it tells you how to handle this in the FAQ.
Regards.
Nov 14 '05 #2
"Timothy Madden" <ba****@rmv.spam.home.ro> wrote in message news:<2s*************@uni-berlin.de>...
[snip]
But I don't have a data type for such a pointer
because the function prototype is in the same time being defined.


You want a function that takes as argument a pointer to a function,
which takes as argument a pointer to a function, which takes as
argument a pointer to a function, which ...

I'm not sure you can do it. I'm not sure you can't do it. But
it's making me dizzy.

I think you'd probably be better off if you defined a functional
member of a class and then had a data member that pointed to the
appropriate other instance of the class.
Socks
Nov 14 '05 #3
"Timothy Madden" <ba****@rmv.spam.home.ro> wrote in message
I'm facing a very strange problem. I need to define a function that takes as argument something like a pointer to itself. It takes as argument a pointer of the same type as a pointer to itself and with possibly other
pointer-to-function value. But I don't have a data type for such a pointer
because the function prototype is in the same time being defined. I hope you are following me ...
Could you have an array/vector/stack of function pointers or objects or
pointers to an abstrat class object? When you want to execute the array,
you step through each element and execute each one. So rather than
"different modules of my app will install a new function to be pointed by
this pointer, and each new function will remember the address of and call
the previous function", you'll insert new function pointers or objects or
pointers to objects into the array.

If you literally want to call a function that returns a function, I think
you can setup a class hierarchy and call a class member function that
returns a new class, and this new class stores a pointer to the original
class.
Actualy I need to define a pointer to a function. Different modules of my
app will install a new function to be pointed by this pointer, and each new function will remember the address of and call the previous function,
forming a chain that exists only in code. I guess the idea is used for
at_exit() function in the library or something like it.

Now I want the function to take as argument a pointer to another instance of the same function type. This will allow for removing functions from the
chain even in a different order than the one functions were installed, by
having each installed function recognize it's own address in the argument
and then return as a result the previous installed function that it knows of
I hope you are following me...

So how could I declare the prototype for a function that takes as argument
and that returns a pointer to the same function type being declared ?


Nov 14 '05 #4
"Timothy Madden" <ba****@rmv.spam.home.ro> wrote in message
news:2s*************@uni-berlin.de...
I'm facing a very strange problem. I need to define a function that takes
as
argument something like a pointer to itself. It takes as argument a
pointer
of the same type as a pointer to itself and with possibly other
pointer-to-function value. But I don't have a data type for such a pointer
because the function prototype is in the same time being defined. I hope
you
are following me ...


You can't do it directly, but you can wrap it in a structure:

struct Selfptr {
Selfptr (*fp)(Selfptr);
};
Nov 14 '05 #5
"Andrew Koenig" <ar*@acm.org> writes:
"Timothy Madden" <ba****@rmv.spam.home.ro> wrote in message
news:2s*************@uni-berlin.de...
I'm facing a very strange problem. I need to define a function that takes
as
argument something like a pointer to itself. It takes as argument a
pointer
of the same type as a pointer to itself and with possibly other
pointer-to-function value. But I don't have a data type for such a pointer
because the function prototype is in the same time being defined. I hope
you
are following me ...


You can't do it directly, but you can wrap it in a structure:

struct Selfptr {
Selfptr (*fp)(Selfptr);
};


Unfortunately, this thread is cross-posted to comp.lang.c and
comp.lang.c++. In C, you need:

struct Selfptr {
struct Selfptr (*fp)(struct Selfptr);
};

(gcc gives me "warning: parameter has incomplete type".)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6
"Timothy Madden" <ba****@rmv.spam.home.ro> wrote in message
news:2s*************@uni-berlin.de...
[Cross-posted to: comp.lang.c, comp.lang.c++]

Hello all

I'm facing a very strange problem. I need to define a function that takes as argument something like a pointer to itself. It takes as argument a pointer of the same type as a pointer to itself and with possibly other
pointer-to-function value. But I don't have a data type for such a pointer because the function prototype is in the same time being defined. I hope you are following me ...


I think I see what you are trying to do. I once had to implement a form
with fields that would a number of validation functions attached. If any
one failed the chain was stopped, otherwise they would have to run down
the list of functions.

We implemented the validations as separate functions, and put them all
in a validations array. The array was of type "pointer to function". Now
all you need to is set the list of validations to the array offset. Call
Val101, Val5, Val72, etc. Of course the names were #defined to something
meaningful like ALL_CHARS, ZIPCODE, TELEPHONE_NUM, etc.

In your case, you pass a (doubly) linked list of array offsets and add
or remove items as needed. You could even restart the loop by setting
curr_ftn to head.

--
Mabden
Nov 14 '05 #7

"Andrew Koenig" <ar*@acm.org> wrote in message
news:SU********************@bgtnsc04-news.ops.worldnet.att.net...
"Timothy Madden" <ba****@rmv.spam.home.ro> wrote in message
news:2s*************@uni-berlin.de...
I'm facing a very strange problem. I need to define a function that takes as
argument something like a pointer to itself. It takes as argument a
pointer
of the same type as a pointer to itself and with possibly other
pointer-to-function value. But I don't have a data type for such a pointer because the function prototype is in the same time being defined. I hope
you
are following me ...


You can't do it directly, but you can wrap it in a structure:

struct Selfptr {
Selfptr (*fp)(Selfptr);
};


I think this is the best way I can find to do it ... with some operators
overloaded in C++
Thank you

Does anyone think that there should be a special class of "void code
pointes" in C/C++ ?
A kind of pointer that could only be assigned function addresses, without
the pointer being of a specific function type.

Timothy Madden
Romania
-----------------------------------------------
And I don't wanna miss a thing
Nov 14 '05 #8
Timothy Madden wrote:
[...]
Does anyone think that there should be a special class of "void code
pointes" in C/C++ ?
A kind of pointer that could only be assigned function addresses, without
the pointer being of a specific function type.


What problem would you solve with it that cannot otherwise be solved
with what C++ already has?

V
Nov 14 '05 #9

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:5i****************@newsread1.dllstx09.us.to.v erio.net...
Timothy Madden wrote:
[...]
Does anyone think that there should be a special class of "void code
pointes" in C/C++ ?
A kind of pointer that could only be assigned function addresses, without the pointer being of a specific function type.


What problem would you solve with it that cannot otherwise be solved
with what C++ already has?


Well ... It's like 'what problem would I solve with C++ that cannot
otherwise be solved with what C already has ?'
I think this could help making things clear in a good program

Timothy Madden
Romania
-----------------------------------------------------------
And I don't wanna miss a thing
Nov 14 '05 #10
Hi Timothy,
[Timothy Madden:]
Does anyone think that there should be a special class of "void code
pointes" in C/C++ ?
A kind of pointer that could only be assigned function addresses,
without the pointer being of a specific function type. [Victor Bazarov:]What problem would you solve with it that cannot otherwise be solved
with what C++ already has?

[Timothy Madden:] Well ... It's like 'what problem would I solve with C++ that cannot
otherwise be solved with what C already has ?'
I think the question here is what kind of "problem" we are talking
about. We could throw out some stuff from C and would still be able
to solve the same problems.
OTOH, C++ often speeds up development. And I think that it is here
that there is no bottleneck and no obvious gap which this addition
would help do away with. IMO, that is what Victor was addressing.

I think this could help making things clear in a good program


How so? You would get yourself function pointers without type
which would make it extremely hard to be sure that you are
calling a nondescript function correctly. IMO, a "collection" of
functions with the same return type and parameter list is the
only sensible case for the use of a function pointer. It is
clearer to provide wrapper functions for functions who do not
exactly fit than to have to take care which function now is
"behind" the "void *"-like function pointer.
Even if you could somehow derive return type and parameter list,
the code would not necessarily become clearer.
Cheers
Michael

Nov 14 '05 #11
On Mon, 4 Oct 2004 14:53:00 +0200, "Timothy Madden"
<ba****@rmv.spam.home.ro> wrote:

"Andrew Koenig" <ar*@acm.org> wrote in message
news:SU********************@bgtnsc04-news.ops.worldnet.att.net...
"Timothy Madden" <ba****@rmv.spam.home.ro> wrote in message
news:2s*************@uni-berlin.de...
> I'm facing a very strange problem. I need to define a function thattakes > as
> argument something like a pointer to itself. It takes as argument a
> pointer
> of the same type as a pointer to itself and with possibly other
> pointer-to-function value. But I don't have a data type for such apointer > because the function prototype is in the same time being defined. I hope
> you
> are following me ...


You can't do it directly, but you can wrap it in a structure:

struct Selfptr {
Selfptr (*fp)(Selfptr);
};


I think this is the best way I can find to do it ... with some operators
overloaded in C++
Thank you

Does anyone think that there should be a special class of "void code
pointes" in C/C++ ?
A kind of pointer that could only be assigned function addresses, without
the pointer being of a specific function type.


Well, it is legal to cast function pointer types to other types. If I
want a generic pointer to function, I just use void(*)(void). Just
make sure you cast the function back to its original type before
calling it!

Tom
Nov 14 '05 #12
Tom Widmer wrote:
[SNIP]
Well, it is legal to cast function pointer types to other types.

[SNIP]

I guess you meant other function pointer types?

--
Attila aka WW
Nov 14 '05 #13
On Tue, 5 Oct 2004 14:15:55 +0300, "Attila Feher"
<at**********@lmf.ericsson.se> wrote:
Tom Widmer wrote:
[SNIP]
Well, it is legal to cast function pointer types to other types.

[SNIP]

I guess you meant other function pointer types?


Right.

Tom
Nov 14 '05 #14

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

Similar topics

10
by: Steve Goldman | last post by:
Hi, I am trying to come up with a way to develop all n-length permutations of a given list of values. The short function below seems to work, but I can't help thinking there's a better way. ...
2
by: SainTiss | last post by:
Hi, If you've got a template class with lots of methods, and then you've got a type which works with the template, except for one method... What you need to do there is specialize the...
13
by: Timothy Madden | last post by:
Hello all I'm facing a very strange problem. I need to define a function that takes as argument something like a pointer to itself. It takes as argument a pointer of the same type as a pointer...
7
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
37
by: Protoman | last post by:
Hi!!! Protoman here, I need to write an infinite precision number class b/c I want to compute pi. Give me some sample code. Also, when I run my program and it computes pi, will my computer freeze...
1
by: Jon Slaughter | last post by:
I've managed to put together a template class that basicaly creates a recursive tree that lets you easily specify the "base" class of that tree and and ending notes and lets you stop the recursive...
74
by: lovecreatesbeauty | last post by:
My small function works, but I have some questions. And I want to listen to you on How it is implemented? 1. The function does not check if parameter x is larger or smaller than parameter y. ...
20
by: dspfun | last post by:
I've come a across a program that declares the following data structure. typedef struct node { struct node *next; } node; It looks like a recursive data structure but I'm having trouble...
10
by: lkrich7 | last post by:
hello, I wrote the following definition: typedef vector< pair<char, TrieNode* TrieNode; but it can't be compiled, because "TrieNode" is not declared in fact my aim is: struct Pair; typedef...
3
by: from.future.import | last post by:
Hi, I encountered garbage collection behaviour that I didn't expect when using a recursive function inside another function: the definition of the inner function seems to contain a circular...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.