Note that there are some explanatory texts on larger screens.

plurals
  1. POMapped functors to member functions losing scope
    text
    copied!<p>I'd like to be able to call some member functions from different classes which all have the same general syntax and base class. Something along the lines of</p> <pre><code>class A: public BaseClass { public: A(); ~A(); int DoFoo(); int DoBar(); int DoBarBar(); }; class B : public BaseClass { public: B(); ~B(); int DoSomething(); int DoSomethingElse(); int DoAnother(); }; </code></pre> <p>Where I could potentially places the member functions from both classes into one map so that I could have something like</p> <pre><code> key value "Option1" *DoFoo() "Option2" *DoSomething() "Option3" *DoFoo() ... ... "Option6" *DoAnother() </code></pre> <p>Where I could call a function to return a value based on what option I chose, regardless of what class the function belongs to.</p> <p>Through some searching, I tried to implement my own mapped set of functors. However, the map retains the address of the functor, but the functions within become null.</p> <p>Here are my functor declarations which store a class object and a function pointer</p> <pre><code> #include &lt;stdio.h&gt; #include &lt;vector&gt; #include &lt;algorithm&gt; #include &lt;map&gt; #include &lt;string&gt; ////////////////////////////////////////////////////////////// //Functor Classes ////////////////////////////////////////////////////////////// class TFunctor { public: virtual void operator()()=0; // call using operator virtual int Call()=0; // call using function }; // derived template class template &lt;class TClass&gt; class TSpecificFunctor : public TFunctor { private: int (TClass::*fpt)(); // pointer to member function TClass* pt2Object; // pointer to object public: // constructor - takes pointer to an object and pointer to a member and stores // them in two private variables TSpecificFunctor(TClass* _pt2Object, int(TClass::*_fpt)()) { pt2Object = _pt2Object; fpt=_fpt; }; // override operator "()" virtual void operator()() { (*pt2Object.*fpt)();}; // execute member function // override function "Call" virtual int Call() {return (*pt2Object.*fpt)();}; // execute member function }; typedef std::map&lt;std::string, TFunctor*&gt; TestMap; ////////////////////////////////////////////////////////////// //Test Classes ////////////////////////////////////////////////////////////// //Base Test class class base { public: base(int length, int width){m_length = length; m_width = width;} virtual ~base(){} int area(){return m_length*m_width;} int m_length; int m_width; }; //Inherited class which contains two functions I would like to point to class inherit:public base { public: inherit(int length, int width, int height); ~inherit(); int volume(){return base::area()*m_height;} int area2(){return m_width*m_height;} int m_height; TestMap m_map; }; </code></pre> <p>where my inherit class constructor looks like:</p> <pre><code>inherit::inherit(int length, int width, int height):base(length, width) { m_height = height; TSpecificFunctor&lt;inherit&gt; funcA(this, &amp;inherit::volume); m_map["a"] = &amp;funcA; TSpecificFunctor&lt;inherit&gt; funcB(this, &amp;inherit::area2); m_map["b"] = &amp;funcB; } </code></pre> <p>Which is where I am mapping two functions into a map. Things still look okay in the above function in terms of memory address and function pointers.</p> <p>I then try to create an instance of inherit in a new class...</p> <pre><code>class overall { public: overall(); ~overall(){} inherit *m_inherit; TestMap m_mapOverall; }; overall::overall() { m_inherit = new inherit(3,4,5); TestMap tempMap = m_inherit-&gt;m_map; int i = 0; } </code></pre> <p>Here when I look at the values of m_inherit->m_map, I notice that the keys are still consistent, however the memory addresses of the functions which I tried to point to have disappeared.</p> <p>I haven't had much experience with functors but from my understanding, it is able to retain states, which I assume means that I can call member functions outside of its class. But I'm starting to think that my member functions disappear because it is out of scope.</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