Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the merit of the "function" type (not "pointer to function")
    primarykey
    data
    text
    <p>Reading the C++ Standard, i see that there are "function" types and "pointer to function" types:</p> <pre><code>typedef int func(int); // function typedef int (*pfunc)(int); // pointer to function typedef func* pfunc; // same as above </code></pre> <p>I have never seen the function types used outside of examples (or maybe i didn't recognize their usage?). Some examples:</p> <pre><code>func increase, decrease; // declares two functions int increase(int), decrease(int); // same as above int increase(int x) {return x + 1;} // cannot use the typedef when defining functions int decrease(int x) {return x - 1;} // cannot use the typedef when defining functions struct mystruct { func add, subtract, multiply; // declares three member functions int member; }; int mystruct::add(int x) {return x + member;} // cannot use the typedef int mystruct::subtract(int x) {return x - member;} int main() { func k; // the syntax is correct but the variable k is useless! mystruct myobject; myobject.member = 4; cout &lt;&lt; increase(5) &lt;&lt; ' ' &lt;&lt; decrease(5) &lt;&lt; '\n'; // outputs 6 and 4 cout &lt;&lt; myobject.add(5) &lt;&lt; ' ' &lt;&lt; myobject.subtract(5) &lt;&lt; '\n'; // 9 and 1 } </code></pre> <p>Seeing that the function types support syntax that doesn't appear in C (declaring member functions), i guess they are not just a part of C baggage that C++ has to support for backward compatibility.</p> <p>So is there any use for function types, other than demonstrating some funky syntax?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload