<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Basisklassenzeiger als Template Argument?]]></title><description><![CDATA[<p>Hi</p>
<p>Wenn ich einer Templatefunktion Basisklassenzeiger auf abgeleitete Objekte übergebe werden diese als Basisklassen ausgewertet.<br />
Kann ich das irgendwie umgehen, so dass er erkennt, welche Subklasse ein Objekt hat?<br />
Grund, ich probier type traits aus( : <a href="http://www.boost.org/doc/html/boost_typetraits/reference.html#boost_typetraits.is_same" rel="nofollow">Boost Type Traits</a> )</p>
<p>Minimalbeispiel zum Testen:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
#include &lt;boost/type_traits/is_same.hpp&gt;

using namespace std;

class BaseTag{};
class Sub1Tag: public BaseTag{};
class Sub2Tag: public BaseTag{};

template&lt; typename ClassT &gt;
struct ClassTraits
{
    typedef typename ClassT::ClassCategory ClassCategory;
};

class Base
{
public:
    typedef BaseTag ClassCategory;
    virtual void do_it() const
    {
        cout &lt;&lt; &quot;Hello from Base&quot; &lt;&lt; endl;
    }
};

class Sub1: public Base
{
public:
    typedef Sub1Tag ClassCategory;
    virtual void do_it() const
    {
        cout &lt;&lt; &quot;Hello from Sub1&quot; &lt;&lt; endl;
    }
};

class Sub2: public Base
{
public:
    typedef Sub2Tag ClassCategory;
    virtual void do_it() const
    {
        cout &lt;&lt; &quot;Hello from Sub2&quot; &lt;&lt; endl;
    }
};

template&lt; typename LeftT, typename RightT &gt;                         // Dieses Template wird 2 mal mit Base instanziert...
bool is_same_type( const LeftT* lhs, const RightT* rhs )
{
    lhs-&gt;do_it();          
    rhs-&gt;do_it();
    return boost::is_same&lt;
        typename ClassTraits&lt;LeftT&gt;::ClassCategory,
        typename ClassTraits&lt;RightT&gt;::ClassCategory
                            &gt;::value;
}

int main(int argc, char *argv[])
{
    Base* p1 = new Sub1();
    Base* p2 = new Sub2();
    p1-&gt;do_it();
    p2-&gt;do_it();
    if( is_same_type( p1, p2 ) )                                        // ...mit diesem Aufruf
    {
        cout &lt;&lt; &quot;Are the same&quot; &lt;&lt; endl;
    }
    else
    {
        cout &lt;&lt; &quot;Not the same&quot; &lt;&lt; endl;
    }
    delete p1;
    delete p2;

    return EXIT_SUCCESS;
}
</code></pre>
<blockquote>
<p>Hello from Sub1<br />
Hello from Sub2<br />
Hello from Sub1<br />
Hello from Sub2<br />
Are the same</p>
</blockquote>
<p>Danke im voraus für jeden noch so kleinen Tipp<br />
Grüsse</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/172986/basisklassenzeiger-als-template-argument</link><generator>RSS for Node</generator><lastBuildDate>Fri, 18 Sep 2026 03:28:02 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/172986.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 10 Feb 2007 14:57:26 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Basisklassenzeiger als Template Argument? on Sat, 10 Feb 2007 15:12:52 GMT]]></title><description><![CDATA[<p>Hi</p>
<p>Wenn ich einer Templatefunktion Basisklassenzeiger auf abgeleitete Objekte übergebe werden diese als Basisklassen ausgewertet.<br />
Kann ich das irgendwie umgehen, so dass er erkennt, welche Subklasse ein Objekt hat?<br />
Grund, ich probier type traits aus( : <a href="http://www.boost.org/doc/html/boost_typetraits/reference.html#boost_typetraits.is_same" rel="nofollow">Boost Type Traits</a> )</p>
<p>Minimalbeispiel zum Testen:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
#include &lt;boost/type_traits/is_same.hpp&gt;

using namespace std;

class BaseTag{};
class Sub1Tag: public BaseTag{};
class Sub2Tag: public BaseTag{};

template&lt; typename ClassT &gt;
struct ClassTraits
{
    typedef typename ClassT::ClassCategory ClassCategory;
};

class Base
{
public:
    typedef BaseTag ClassCategory;
    virtual void do_it() const
    {
        cout &lt;&lt; &quot;Hello from Base&quot; &lt;&lt; endl;
    }
};

class Sub1: public Base
{
public:
    typedef Sub1Tag ClassCategory;
    virtual void do_it() const
    {
        cout &lt;&lt; &quot;Hello from Sub1&quot; &lt;&lt; endl;
    }
};

class Sub2: public Base
{
public:
    typedef Sub2Tag ClassCategory;
    virtual void do_it() const
    {
        cout &lt;&lt; &quot;Hello from Sub2&quot; &lt;&lt; endl;
    }
};

template&lt; typename LeftT, typename RightT &gt;                         // Dieses Template wird 2 mal mit Base instanziert...
bool is_same_type( const LeftT* lhs, const RightT* rhs )
{
    lhs-&gt;do_it();          
    rhs-&gt;do_it();
    return boost::is_same&lt;
        typename ClassTraits&lt;LeftT&gt;::ClassCategory,
        typename ClassTraits&lt;RightT&gt;::ClassCategory
                            &gt;::value;
}

int main(int argc, char *argv[])
{
    Base* p1 = new Sub1();
    Base* p2 = new Sub2();
    p1-&gt;do_it();
    p2-&gt;do_it();
    if( is_same_type( p1, p2 ) )                                        // ...mit diesem Aufruf
    {
        cout &lt;&lt; &quot;Are the same&quot; &lt;&lt; endl;
    }
    else
    {
        cout &lt;&lt; &quot;Not the same&quot; &lt;&lt; endl;
    }
    delete p1;
    delete p2;

    return EXIT_SUCCESS;
}
</code></pre>
<blockquote>
<p>Hello from Sub1<br />
Hello from Sub2<br />
Hello from Sub1<br />
Hello from Sub2<br />
Are the same</p>
</blockquote>
<p>Danke im voraus für jeden noch so kleinen Tipp<br />
Grüsse</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1226250</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1226250</guid><dc:creator><![CDATA[THX 1138]]></dc:creator><pubDate>Sat, 10 Feb 2007 15:12:52 GMT</pubDate></item><item><title><![CDATA[Reply to Basisklassenzeiger als Template Argument? on Sat, 10 Feb 2007 15:59:13 GMT]]></title><description><![CDATA[<p>Hab das Beispiel noch minimiert:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
#include &lt;boost/type_traits/is_same.hpp&gt;

using namespace std;

class Base {};

class Sub1: public Base {};

class Sub2: public Base {};

template&lt; typename LeftT, typename RightT &gt;
bool is_same_type( const LeftT* lhs, const RightT* rhs )
{
    return boost::is_same&lt; LeftT, RightT &gt;::value;
}

int main(int argc, char *argv[])
{
    Base* p1 = new Sub1();
    Base* p2 = new Sub2();
    Sub1 s1;
    Sub2 s2;
    if( is_same_type( p1, p2 ) )
    {
        cout &lt;&lt; &quot;Are the same&quot; &lt;&lt; endl;
    }
    else
    {
        cout &lt;&lt; &quot;Not the same&quot; &lt;&lt; endl;
    }
    if( is_same_type( &amp;s1, &amp;s2 ) )
    {
        cout &lt;&lt; &quot;Are the same&quot; &lt;&lt; endl;
    }
    else
    {
        cout &lt;&lt; &quot;Not the same&quot; &lt;&lt; endl;
    }
    if( is_same_type( p1, &amp;s1 ) )
    {
        cout &lt;&lt; &quot;Are the same&quot; &lt;&lt; endl;
    }
    else
    {
        cout &lt;&lt; &quot;Not the same&quot; &lt;&lt; endl;
    }
    delete p1;
    delete p2;

    return EXIT_SUCCESS;
}
</code></pre>
<blockquote>
<p>Are the same<br />
Not the same<br />
Not the same</p>
</blockquote>
<p>Schade, das scheint nicht zu gehen..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1226286</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1226286</guid><dc:creator><![CDATA[THX 1138]]></dc:creator><pubDate>Sat, 10 Feb 2007 15:59:13 GMT</pubDate></item><item><title><![CDATA[Reply to Basisklassenzeiger als Template Argument? on Sat, 10 Feb 2007 17:14:48 GMT]]></title><description><![CDATA[<p>Was du suchst heißt RTTI.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1226342</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1226342</guid><dc:creator><![CDATA[lolz]]></dc:creator><pubDate>Sat, 10 Feb 2007 17:14:48 GMT</pubDate></item><item><title><![CDATA[Reply to Basisklassenzeiger als Template Argument? on Sat, 10 Feb 2007 17:38:43 GMT]]></title><description><![CDATA[<p>Hmm..meinst du typeid()?<br />
Die Benutzung davon will ich eben vermeiden..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1226365</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1226365</guid><dc:creator><![CDATA[THX 1138]]></dc:creator><pubDate>Sat, 10 Feb 2007 17:38:43 GMT</pubDate></item><item><title><![CDATA[Reply to Basisklassenzeiger als Template Argument? on Sat, 10 Feb 2007 17:42:55 GMT]]></title><description><![CDATA[<p>Ja, anders geht es nicht wenn der Typ erst zur Laufzeit feststeht.</p>
<p>Wenn du in deinem Beispiel folgendes schreibst, wird dir schnell klar, warum man es nicht zur Compile-Zeit feststellen kann:</p>
<pre><code class="language-cpp">Base* p1 = rand() % 2 ? new Sub1() : new Sub2;
Base* p2 = rand() % 2 ? new Sub1() : new Sub2;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1226368</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1226368</guid><dc:creator><![CDATA[lolz]]></dc:creator><pubDate>Sat, 10 Feb 2007 17:42:55 GMT</pubDate></item><item><title><![CDATA[Reply to Basisklassenzeiger als Template Argument? on Sat, 10 Feb 2007 18:02:20 GMT]]></title><description><![CDATA[<p>Jo, danke.</p>
<p>Dann werd ich anders an die Sache rangehen müssen.<br />
Es gibt doch auch Compile time polymorphismus? Werde mich da noch weiter informieren..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1226384</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1226384</guid><dc:creator><![CDATA[THX 1138]]></dc:creator><pubDate>Sat, 10 Feb 2007 18:02:20 GMT</pubDate></item><item><title><![CDATA[Reply to Basisklassenzeiger als Template Argument? on Sat, 10 Feb 2007 18:14:13 GMT]]></title><description><![CDATA[<p>Gibt es, sieht so aus:</p>
<pre><code class="language-cpp">template&lt; class Derived &gt;
class MyClass
{
 public:
  void stuff()
  {
     Derived* self = static_cast&lt; Derived* &gt;( this );
     self-&gt;do_stuff();
  }
};

class Derived : public MyClass&lt; Derived &gt;
{
  public:
  void do_stuff()
  {
     cout &lt;&lt; &quot;hello world!\n&quot;;
  }
};

template&lt; class Derived &gt;
void handler( MyClass&lt; Derived &gt;&amp; obj )
{
  obj.stuff();
}

int main()
{
  Derived myObj;

  handler( myObj );
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1226395</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1226395</guid><dc:creator><![CDATA[lolz]]></dc:creator><pubDate>Sat, 10 Feb 2007 18:14:13 GMT</pubDate></item><item><title><![CDATA[Reply to Basisklassenzeiger als Template Argument? on Sat, 10 Feb 2007 18:19:27 GMT]]></title><description><![CDATA[<p>Cool danke, das schubst mich mal in ne andere Richtung.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1226399</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1226399</guid><dc:creator><![CDATA[THX 1138]]></dc:creator><pubDate>Sat, 10 Feb 2007 18:19:27 GMT</pubDate></item><item><title><![CDATA[Reply to Basisklassenzeiger als Template Argument? on Sat, 10 Feb 2007 18:25:24 GMT]]></title><description><![CDATA[<p>sry doppel..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1226401</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1226401</guid><dc:creator><![CDATA[THX 1138]]></dc:creator><pubDate>Sat, 10 Feb 2007 18:25:24 GMT</pubDate></item></channel></rss>