Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In order to create a class method that takes a template parameter, yes, it would work almost exactly like a function template. For example:</p> <pre><code>class A { public: template&lt;typename T&gt; void my_function(const T&amp; value) { } }; int main() { A test; test.my_function(5); return 0; } </code></pre> <p>Secondly, for your structure, you can actually turn that into a functor-object that by overloading <code>operator()</code>, lets you call the structure as-if it were a function rather than having to actually call the specific function pointer members inside the structure. For instance, your structure could be re-written to look like this:</p> <pre><code>#include &lt;iostream&gt; template &lt;class ReturnType, class ParameterType&gt; class funcpar { private: ReturnType (*function)(ParameterType); ParameterType parameter; public: funcpar(ReturnType (*func)(ParameterType),ParameterType param): function(func), parameter(param) {} funcpar&amp; operator=(const funcpar&amp; fp); //operator() overloaded to be a function that takes no arguments //and returns type ReturnType ReturnType operator() () { return function(parameter); } }; int sample_func(int value) { return value + 1; } int main() { funcpar&lt;int, int&gt; test_functor(sample_func, 5); //you can call any instance of funcpar just like a normal function std::cout &lt;&lt; test_functor() &lt;&lt; std::endl; return 0; } </code></pre> <p>BTW, you do need the functor object (or your structure, etc.) in order to bind a dynamic parameter to a function before the function is called in C/C++ ... you can't "store" a parameter with an actual function. Binding a parameter to a function is actually called a closure, and in C/C++, creating a closure requires a structure/class or some type of associated data-structure you can use to bind a function with a specific parameter stored in memory that is used only for a specific instance of that function call.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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