Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to assign method member pointer of a subclass?
    text
    copied!<p>My problem is a bit complicated. I have one class (e: Component) which have Ports objects. When a Component create a Port object, it pass one of its methods to the Port constructor.</p> <p>Methods signatures :</p> <pre><code>typedef std::vector&lt;std::string&gt; (Component::*ComponentMethod)(std::vector&lt;std::string&gt;); </code></pre> <p>It works fine if I do :</p> <pre><code>// in class Component std::vector&lt;std::string&gt; Component::test_method( std::vector&lt;std::string&gt; ) { std::cout &lt;&lt; "Hello !\n"; // Do stuff } // Port ctor : Port( ComponentMethod callback ); Port* p = new Port(&amp;Component::test_method); </code></pre> <p>Well... now, my problem is that I make subclasses of class Component, and I don't know how to pass sublcasses methods to a Port.</p> <pre><code>// in class SubtypeComponent std::vector&lt;std::string&gt; SubtypeComponent::test_method2( std::vector&lt;std::string&gt; ) { std::cout &lt;&lt; "Hello !\n"; // Do stuff } // Port ctor : Port( ComponentMethod callback ); Port* p = new Port(&amp;SubtypeComponent::test_method2); // ERROR :'( </code></pre> <p>It seems normal : I guess the compiler expects precisely Component (only) method.</p> <p>--> I'm looking for a solution to "dynamically" assign methods in Ports (but I don't think this is possible)</p> <p>--> Maybe another solution should be usage of templates ? (defining "template methods pointers" instead of "Component methods pointers"), but I'm not sure.</p> <p>Any help would be appreciated :)</p>
 

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