Note that there are some explanatory texts on larger screens.

plurals
  1. POdeleting memory for static variable in C++
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/4872803/deleting-memory-allocated-in-static-function-in-c">deleting memory allocated in static function in C++</a> </p> </blockquote> <p>Hi All,</p> <p>I have C++ class as follows</p> <pre><code>interface myInterface; class anotherClass : public myInterface {}; class myClass { private: myClass() {} ~myClass() {} typedef std::map&lt;string, myInterface* &gt; stringToClass; static stringToClass s_stringToClass; public: static myInterface&amp; getStringToclass(string name); }; </code></pre> <p>in above class for getStringToClass defintion is as follows</p> <pre><code> myInterface&amp; myClass::getStringToClass(string name) { stringToClass::iterator iter; iter = s_stringToClass.find(name); if(iter == s_stringToClass.end()) { typedef stringToClass::value_type stringToClassPair; anotherClass* pothClass = new anotherClass(); s_stringToClass.insert(stringToClassPair(name, pothClass)); return pothClass; } else { return iter-&gt;second; } } </code></pre> <p>now my question is we are allocating memory in static function and returning a pointer of class type, but here i want to retrun a reference as we don't want to give control of pointer to user. And also i don't want to delete immedetily unless user asked to or want to delete at end of program How can we delete memory? As there is only instance of class will be there, as there are only static functions.</p> <p>Thanks for the help.</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.
 

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