Note that there are some explanatory texts on larger screens.

plurals
  1. POIssues with C++ template arguments for inheritance
    primarykey
    data
    text
    <p>I have a questions about C++ templates. More specifally, by using template arguments for inheritance. I am facing strange behaviour in a closed-source 3rd party library. There is a C method</p> <pre><code>factoryReg(const char*, ICallback*) </code></pre> <p>which allows to register a subclass of ICallback and overwrite the (simplified) methods:</p> <pre><code>class ICallback { public: virtual void ENTRY(void* data) = 0; virtual void EXIT(void* data) = 0; const char* getName() { return _name; } const ICallback(const char* name) : _name(name) {} virtual ~ICallback() {} private: const char* _name; }; </code></pre> <p>I have </p> <pre><code> class BaseCallback : public ICallback { public: BaseCallback(const char* name) : ICallback(name) {} virtual void ENTRY(void* data) { std::cout &lt;&lt; "in ENTRY base" &lt;&lt; std::endl; } virtual void EXIT(void* data) { std::cout &lt;&lt; "in EXIT base" &lt;&lt; std::endl; }; class SpecialCallback : public BaseCallback { public: SpecialCallback(const char* name) : BaseCallback(name) {} virtual void ENTRY(void* data) { // actually, it's 3rd party code too - assumed to do something like ... BaseCallback::ENTRY(); } // no redecl. of EXIT(void* data) }; template &lt;typename Base&gt; TemplCallback : public Base { public: TemplCallback(Base&amp; myT) : Base(myT.getName()), _myT(myT) virtual void ENTRY(void* data) { std::cout &lt;&lt; "in ENTRY templ." &lt;&lt; std::endl; _myT.ENTRY(); } virtual void EXIT(void* data) { std::cout &lt;&lt; "in EXIT templ." &lt;&lt; std::endl; _myT.EXIT(); } private: Base&amp; _myT; } </code></pre> <p>Upon registering</p> <pre><code>SpecialCallback spc("validName"); TemplCallback&lt;SpecialCallback&gt; myCallback(spc); factoryReg(spc.getName(), &amp;myCallback); ... // output: "in ENTRY base" // "in EXIT base" </code></pre> <p>the callback somehow does not work (debug output not being put out // breakpoints do not apply).</p> <p>If I omit implementation of the EXIT(void* data) method in my template class TemplCallback - everything works fine! </p> <pre><code>// output: "in ENTRY templ." // "in EXIT base" </code></pre> <p>Is this expected behaviour? I have been told it might be an issue of the MSVC compiler 13.10.6030 I use. Not sure about that.</p> <p>BTW: The template idea presented here might not be the best choice for whatever I am trying to do ;) But I am still interested in the matter itself, regardless about design questions.</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.
 

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