Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>Solution 1</h1> <p>IF you're okay with the decorated name, then you can write a <em>free</em> function template:</p> <pre><code>struct Vehicle {}; struct Aircraft : Vehicle { typedef Vehicle super; }; struct Helicopter : Aircraft { typedef Aircraft super; }; template&lt;typename T&gt; string getClassLineage() { static string lineage = string(typeid(T).name()) +" - " + getClassLineage&lt;typename T::super&gt;(); return lineage; } template&lt;&gt; string getClassLineage&lt;Vehicle&gt;() { static string lineage = string(typeid(Vehicle).name()); return lineage; } int main() { cout &lt;&lt; getClassLineage&lt;Helicopter&gt;() &lt;&lt; endl; return 0; } </code></pre> <p>Output (decorated names):</p> <blockquote> <p>10Helicopter - 8Aircraft - 7Vehicle</p> </blockquote> <p>See at ideone: <a href="http://www.ideone.com/5PoJ0" rel="nofollow">http://www.ideone.com/5PoJ0</a></p> <p>You can strip off the decoration if you want. But it would be compiler specific! <a href="http://www.ideone.com/JexQM" rel="nofollow">Here</a> is a version that makes use of <code>remove_decoration</code> function to strip off the decoration, and then the output becomes :</p> <blockquote> <p>Helicopter - Aircraft - Vehicle</p> </blockquote> <p>By the way, as I said, the implementation of <code>remove_decoration</code> function is a compiler specific; also, this can be written in more correct way, as I don't know all cases which GCC considers, while <a href="http://en.wikipedia.org/wiki/Name_mangling" rel="nofollow">mangling</a> the class names. But I hope, you get the basic idea.</p> <hr> <h1>Solution 2</h1> <p>If you're okay with redefining the function in each derived class, then here is a simple solution:</p> <pre><code>struct Vehicle { string getClassLineage() const { return "Vehicle"; } }; struct Aircraft : Vehicle { string getClassLineage() const { return Vehicle::getClassLineage()+" - Aircraft"; } }; struct Helicopter : Aircraft { string getClassLineage() const { return Aircraft::getClassLineage()+" - Helicopter "; } }; int main() { Helicopter heli; cout &lt;&lt; heli.getClassLineage() &lt;&lt; endl; return 0; } </code></pre> <p>Output:</p> <blockquote> <p>Vehicle - Aircraft - Helicopter </p> </blockquote> <p>See output at ideone: <a href="http://www.ideone.com/Z0Tws" rel="nofollow">http://www.ideone.com/Z0Tws</a></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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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