Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamic C++
    primarykey
    data
    text
    <p>I'm wondering about an idea in my head. I want to ask if you know of any library or article related to this. Or you can just tell me this is a dumb idea and why.</p> <p>I have a class, and I want to dynamically add methods/properties to it at runtime. I'm well aware of the techniques of using composite/command design pattern and using embedded scripting language to accomplish what I'm talking about. I'm just exploring the idea. Not necessary saying that it is a good idea.</p> <pre><code>class Dynamic { public: typedef std::map&lt;std::string, boost::function&lt;void (Dynamic&amp;)&gt; &gt; FuncMap; void addMethod(const std::string&amp; name, boost::function&lt;void (Dynamic&amp;)&gt; func) { funcMap_[name] = func; } void operator[](const std::string&amp; name) { FuncMap::iterator funcItr = funcMap_.find(name); if (funcItr != funcMap_.end()) { funcItr-&gt;second(*this); } } private: FuncMap funcMap_; }; void f(Dynamic&amp; self) { doStuffWithDynamic(self); } int main() { Dynamic dyn; dyn.addMethod("f", boost::bind(&amp;f, _1)); dyn["f"]; // invoke f } </code></pre> <p>The idea is that I can rebind the name "f" to any function at runtime. I'm aware of the performance problem in string lookup and boost::function vs. raw function pointer. With some hard work and non-portable hack I think I can make the performance problem less painful.</p> <p>With the same kind of technique, I can do "runtime inheritance" by having a "v-table" for name lookup and dispatch function calls base on dynamic runtime properties.</p> <p>If just want to tell me to use smalltalk or Objective-C, I can respect that but I love my C++ and I'm sticking with it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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. 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