Any (standard) way to have anonymous functions in C?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Any (standard) way to have anonymous functions in C?
I have a program I'm working on, in which I'd like to have structures containing pointers to anonymous functions. Basically the structure type would contain a function pointer as a member; and when each individual structure was initalized, I would feed the address of an anonymous function into its pointer.
This way I could have some degree of polymorphism; i.e. I could call the correct function required by a given structure instance, without needing different function names, switch statements, etc. Instead of calling foo() for a foo_s struct and bar() for a bar_s struct, I could call (some_foo->func)() or (some_bar->func)().
Is there a sane, commonplace way to do this in C? Is it even possible without implementing half of C++? Failing that, are there better ways ot have this kind of polymorphism?
Thanks very much, but that doesn't seem to help a whole lot. With just function pointers, I would still have to implement a large number of named functions.
Is there any way to create anonymous functions in C, that is not dependent on the compiler?
You still have to write the functions anyway. You could call them classA_function1, classA_function2, classB_function1 etc. If you really want them to be anonymous then you might as well use C++.
I wanted them to be anonymous because that could allow for prettier, more readable code. (Or so I thought anyway.) I can't use C++ without porting the entire existing code base.
Thanks though!
Last edited by Gullible Jones; 05-04-2013 at 06:51 PM.
This way I could have some degree of polymorphism; i.e. I could call the correct function required by a given structure instance, without needing different function names, switch statements, etc. Instead of calling foo() for a foo_s struct and bar() for a bar_s struct, I could call (some_foo->func)() or (some_bar->func)().
I have a program I'm working on, in which I'd like to have structures containing pointers to anonymous functions. Basically the structure type would contain a function pointer as a member; and when each individual structure was initalized, I would feed the address of an anonymous function into its pointer.
This way I could have some degree of polymorphism; i.e. I could call the correct function required by a given structure instance, without needing different function names, switch statements, etc. Instead of calling foo() for a foo_s struct and bar() for a bar_s struct, I could call (some_foo->func)() or (some_bar->func)().
Is there a sane, commonplace way to do this in C? Is it even possible without implementing half of C++? Failing that, are there better ways ot have this kind of polymorphism?
Others have already answered your question so I'll just add that the usual way is to declare those functions static so they are not exported to other translation units. But yes, you'll have to give them names, but as psionl0 pointed, this does not have to be that bad since they can be named after the callback name.
I do not really understand it, it is exactly how c++ works: you will have classes, member functions, you can also have virtual functions, and inheritance and finally this will do exactly what you need.
You could call always the correct function because every class instance will "know" himself.
I do not really understand why do you want to build a similar structure (reinvent the wheel?), it is already working and available.
Otherwise (without C++, in c) you must implement all those functions one by one with some names. You can than put them (or their pointers) in an array, shared lib or whatever you want, and use them.
The third possibility could have been a c++ to c translator...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.