Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The call of the function "function" became kind of context sensitive. I suppose, this trick can be exploited to support <a href="http://en.wikipedia.org/wiki/Subject-oriented_programming" rel="nofollow noreferrer">subject-oriented programming</a>. </p> <p>Subject-oriented programming is based on the observation that properties of an object are not inherent to the object itself, but depend on who perceive that object. For example, from the point of view of human, tree is not food, but from the point of view of termite, tree is food. Object-oriented paradigm doesn't support this observation directly, and people often come to complex unnatural designs, because they try to incorporate all different subjective views of an object into one entity ("class"), following thoughtlessly OOP guidelines.</p> <p>So, let's try to state subjective perceptions explicitly, using the trick in question to get context sensitivity.</p> <pre><code>template&lt;class FoodSource&gt; class FoodFrom {}; //forward declarations class Tree; class Termite; class Human; //property "food" of a tree template&lt;&gt; class FoodFrom&lt;Tree&gt; { public: FoodFrom(Tree&amp; _tree): tree(_tree) {} //termite perception of tree as food operator FoodFor&lt;Termite&gt;() { int happiness_increase = 5; tree.mass -= 10; return FoodFor&lt;Termite&gt;(happiness_increase); } //human perception of tree as food operator FoodFor&lt;Human&gt;() { int happiness_increase = 0; return FoodFor&lt;Human&gt;(happiness_increase); } private: Tree&amp; tree; }; //property "food" of a termite template&lt;&gt; class FoodFrom&lt;Termite&gt; { public: FoodFrom(Termite&amp; _termite): termite(_termite) {} //human perception of termite as food operator FoodFor&lt;Human&gt;() { int happiness_increase = -100; //apparently, the termite ought to be terminated due to such a violent act termite.~Termite(); return FoodFor&lt;Human&gt;(happiness_increase); } private: Termite&amp; termite; }; //simple class FoodFor, just for demonstration purposes class FoodBase { public: FoodBase(int _value) : value(_value) {} int value; }; template&lt;class T&gt; class FoodFor: public FoodBase { public: FoodFor(): FoodBase(0) {} FoodFor(int _value) : FoodBase(_value) {} }; class AliveBeing { public: AliveBeing(): happiness(100) {} bool is_happy() { return happiness &gt; 0; } void eat() { happiness += getMeal()-&gt;value; } private: int happiness; virtual FoodBase* getMeal() = 0; }; class Tree: public AliveBeing { public: FoodFrom&lt;Tree&gt; getFood(); //see definition below float mass; //... private: //we don't call getMeal for a tree in this demo virtual FoodBase* getMeal() { return NULL; } }; class Termite: public AliveBeing { public: FoodFrom&lt;Termite&gt; getFood(); //see definition below FoodFor&lt;Termite&gt; meal; private: virtual FoodBase* getMeal() { return &amp;meal; } }; class Human: public AliveBeing { public: FoodFor&lt;Human&gt; meal; private: virtual FoodBase* getMeal() { return &amp;meal; } }; //return proxy "FoodFrom" to "overload" return type FoodFrom&lt;Tree&gt; Tree::getFood() { return FoodFrom&lt;Tree&gt;(*this); } FoodFrom&lt;Termite&gt; Termite::getFood() { return FoodFrom&lt;Termite&gt;(*this); } //usage Tree tree; Termite funny_bug; //funny_bug gets its perceived value of eating tree funny_bug.meal = tree.getFood(); funny_bug.eat(); if(funny_bug.is_happy()) funny_bug.goFindThirdPlace(); //... Human joel; //joel get its perceived value of eating tree joel.meal = tree.getFood(); joel.eat(); //... if(joel.see(funny_bug)) { joel.meal = funny_bug.getFood(); joel.eat(); } if(joel.is_happy()) joel.writeAnotherGreatArticle(); </code></pre> <p>Note that the tree doesn't know what eats it.</p> <p>(great question indeed, made me reflect upon it much)</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