C/C++ doesn't allow this, which means all those great tricks with closures in Perl and similar languages can't be done.

Pshaw. Go look in stdlib.h and figure out how qsort manages to call a function through a pointer to that function.

That is the key element to figuring out how to do anything that you can do with closures. What C won't do for you is bind an environment around a pointer to a function, or do your memory management for you. But nothing stops you from defining a struct that contains a pointer to a function and a pointer to an environment variable, and then a function that takes said struct and an argument, then calls the anonymous function passing in both environment and argument.

With some elbow grease you can then code by hand literally anything that you can do with closures. With the right macros you can hide most of the ugliness.

And if you don't want to do your own memory management, then don't.


In reply to Re: Re^7: OO Getters/Setters by tilly
in thread OO Getters/Setters by theAcolyte

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.